Tutorials

How to Handle Tool Search Results in TypeScript — OpenRouter's New Responses API Schema

OpenRouter's Responses API now documents another tool-search output schema. TypeScript applications that assume every output item is a text message can fail when new variants appear.

Your TypeScript application sends a request to an AI model through OpenRouter. The API returns a response, and the application reads its output.

For a simple text-generation request, the code might look like this:

const message = response.output[0];

console.log(message.content[0].text);

It works when the first output item is an assistant message containing text.

But what happens when the model searches for a tool before generating its answer?

The first output item may no longer be a message. It may represent a tool operation, and the expected content property may not exist.

On November 29, 2026, OpenRouter added a new schema named OutputItemToolSearchCall to its API specification.

The change affects the Responses API and several Batch API operations. Related response unions and streaming event schemas were updated as well.

The important detail is not that tool searching suddenly became possible. OpenRouter already supported tool discovery.

What changed is the documented structure of the responses applications may receive.

For TypeScript integrations, this is a good reason to reconsider assumptions about response.output.

Why the output array is not necessarily a message array

OpenRouter’s Responses API returns an output array containing the items produced during a response.

A normal assistant message can look like this:

{
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "output_text",
      "text": "The operation completed."
    }
  ]
}

This is a simplified example showing the relevant fields.

The type property identifies the kind of item.

But a response can also contain tool calls, reasoning items, and other structured output.

That makes this assumption unsafe:

const text = response.output[0].content[0].text;

It assumes that the array is populated, that its first item has a content array, and that the first content element contains text.

None of those conditions is guaranteed for every possible response.

The new tool-search schema makes the problem more apparent.

OpenRouter’s November 29 changelog lists OutputItemToolSearchCall as a new schema used by:

  • POST /responses
  • POST /batches
  • GET /batches/{id}
  • GET /batches/{id}/results

It also records new variants in the response and streaming-event schema unions.

This is a schema-level addition. The changelog does not provide a complete field-by-field example of the new item, so it would be premature to invent required properties for a dedicated parser.

We can still make the application handle such additions safely.

Start by treating external JSON as unknown

TypeScript types describe values during development. They do not validate JSON received from an HTTP response.

Consider:

const data = await response.json();

Depending on your TypeScript environment and declarations, data may not have a useful validated type.

Assigning a custom interface does not establish that the server returned the expected structure.

A safer approach begins with unknown.

type JsonObject = Record<string, unknown>;

function isRecord(value: unknown): value is JsonObject {
  return (
    typeof value === "object" &&
    value !== null &&
    !Array.isArray(value)
  );
}

type OutputItem = JsonObject & {
  type: string;
};

function isOutputItem(value: unknown): value is OutputItem {
  return (
    isRecord(value) &&
    typeof value.type === "string"
  );
}

This guard does not claim to validate every property of every Responses API item.

It checks only what our application needs before inspecting the item further.

That distinction matters.

A general structural check and a complete validator for OutputItemToolSearchCall are different things.

The former can work without knowing every detail of the newly introduced schema.

The latter should be built against the actual current OpenAPI definition.

Handle known items without rejecting unknown ones

Now write a function that inspects one output item.

function handleOutputItem(value: unknown): void {
  if (!isOutputItem(value)) {
    console.warn("Invalid output item", value);
    return;
  }

  switch (value.type) {
    case "message": {
      if (!Array.isArray(value.content)) {
        return;
      }

      for (const part of value.content) {
        if (
          isOutputItem(part) &&
          part.type === "output_text" &&
          typeof part.text === "string"
        ) {
          console.log("Assistant:", part.text);
        }
      }

      break;
    }

    case "function_call": {
      if (
        typeof value.name === "string" &&
        typeof value.arguments === "string"
      ) {
        console.log("Function requested:", value.name);
        console.log("Arguments:", value.arguments);
      }

      break;
    }

    default:
      console.info(
        "Additional output item:",
        value.type
      );
  }
}

The code recognizes two familiar item types.

Messages are inspected for text content.

Function calls are identified without executing them.

Everything else goes through the default branch.

Why not throw an error when an unknown type appears?

Because the API may introduce additional item variants without invalidating the entire response.

The application might not understand a new tool-search item yet, but that does not necessarily prevent it from reading a subsequent assistant message.

Rejecting the entire response would turn an additive schema change into an application failure.

At the same time, silently discarding all unknown items is not ideal.

If an item contains information your application needs to complete a workflow, ignoring it may leave the operation unfinished.

For that reason, the example logs unexpected types. A production integration can record sanitized diagnostics and decide which unfamiliar items require explicit support.

It should not automatically execute a tool merely because an unfamiliar item contains a tool-related property.

Actual function execution should remain limited to known, authorized tools with validated arguments.

Process the complete response

The next step is to inspect the output array.

function handleResponse(data: unknown): void {
  if (!isRecord(data) || !Array.isArray(data.output)) {
    throw new Error("Unexpected Responses API structure");
  }

  for (const item of data.output) {
    handleOutputItem(item);
  }
}

This handles multiple output items without assuming the first one is the final answer.

It also avoids casting the entire response to a type that has never been validated.

If you use OpenRouter’s official TypeScript SDK, its generated types can provide a more detailed description of the supported API schemas.

Runtime checks remain useful when data crosses an external boundary, particularly when compatibility with newer output types matters.

Streaming needs the same consideration

The November 29 change also affects OutputItemAddedEvent and OutputItemDoneEvent.

A streaming integration cannot assume that every event contains a text fragment.

For example, after parsing an SSE event into an object, the application can identify completed output items separately:

function handleStreamEvent(event: unknown): void {
  if (!isRecord(event)) {
    return;
  }

  if (event.type === "response.output_item.done") {
    handleOutputItem(event.item);
  }
}

This is an event handler, not a complete SSE parser. The surrounding application must still read the stream, separate events, and parse their JSON payloads.

There is also a difference between an item being added and an item being finished.

A response.output_item.added event may contain an item that is not yet complete.

Processing it as though all its properties were finalized can produce misleading results.

For applications that only need completed output items, handling the corresponding done event is often simpler.

For interfaces that display intermediate progress, the added and completed states should be represented separately.

Does this mean Tool Search was introduced on November 29?

No.

OpenRouter already documented tool discovery through its openrouter:tool_search server tool.

Developers can mark function definitions with:

{
  "defer_loading": true
}

When the model needs a deferred tool, the tool-search mechanism can locate and reveal a matching definition.

This reduces the need to include every tool’s full specification in the model’s initial context.

The November 29 addition concerns how tool-search activity is represented in OpenRouter’s API schema.

That is different from announcing the tool-search mechanism itself.

A schema update may also arrive before every SDK, example, or application has been updated to recognize the corresponding type.

For applications consuming Responses API output, the practical lesson is straightforward.

Do not assume that every item contains text.

Inspect its type, validate the properties you intend to use, and distinguish known operations from unfamiliar output.

If a new schema appears, your application should be able to recognize that it encountered something new without misinterpreting it as an ordinary assistant message.

For an integration handling real tool execution, this is especially important.

A response parser should not crash simply because the API gained another output variant.

But it also should not treat an unknown tool-related object as permission to execute arbitrary code.

The correct behavior is to preserve the distinction between parsing, displaying, and executing.

External Sources used for this article

Related Articles

View all →