← Blog

Claude API Tool Use: Parsing JSON Output Reliably

2026-11-24 · 5 min read · SubToAPI Team

When you ask Claude to use tools, the model doesn't return plain text — it returns structured content blocks, one of which is a tool_use block containing a JSON object with the arguments Claude wants to call your function with. Parsing that output correctly, especially under streaming, with malformed input, or with nested schemas, is where most tool-use integrations break. This article walks through the actual shape of the response, the common parsing failures, and how to handle them defensively.

The short answer: Claude's tool use output is not raw text you regex out of a message — it's a structured input object on a tool_use content block, already parsed as JSON by the API. Your job is to validate it against your schema, handle the case where Claude asks for multiple tools in one turn, and correctly reassemble it if you're consuming a stream.

The actual shape of a tool_use response

A non-streaming response with tool use looks like this:

{
  "id": "msg_01...",
  "role": "assistant",
  "content": [
    { "type": "text", "text": "Let me check the weather." },
    {
      "type": "tool_use",
      "id": "toolu_01A...",
      "name": "get_weather",
      "input": { "location": "Lisbon", "unit": "celsius" }
    }
  ],
  "stop_reason": "tool_use"
}

Three things matter here:

  1. content is an array. You'll often get a text block and a tool_use block in the same response. Don't assume content[0] is the tool call.
  2. input is already a JSON object, not a string. The API parses it for you — you don't need JSON.parse() on it in the non-streaming case.
  3. stop_reason is "tool_use", not "end_turn". This is your signal that Claude is waiting for a tool result, not finishing the conversation.

A safe parsing pattern in JavaScript:

const toolCalls = response.content.filter(
  (block) => block.type === "tool_use"
);

for (const call of toolCalls) {
  const { name, input, id } = call;
  // input is already a parsed object
  const result = await dispatchTool(name, input);
  toolResults.push({
    type: "tool_result",
    tool_use_id: id,
    content: JSON.stringify(result)
  });
}

Notice you always filter by type, never by position. If Claude calls two tools in one turn, you'll get two tool_use blocks, and you need to return two tool_result blocks in your next message — matched by tool_use_id, not by order.

Streaming makes the JSON arrive in fragments

This is where most parsing bugs actually live. If you stream, the input field doesn't arrive as a single JSON object — it arrives as partial text deltas you have to accumulate yourself.

The event sequence for a tool call under streaming looks like:

content_block_start   -> { type: "tool_use", id: "toolu_01A", name: "get_weather", input: {} }
content_block_delta    -> { type: "input_json_delta", partial_json: "{\"locat" }
content_block_delta    -> { type: "input_json_delta", partial_json: "ion\": \"Lis" }
content_block_delta    -> { type: "input_json_delta", partial_json: "bon\"}" }
content_block_stop

You have to concatenate partial_json strings across every delta for that content block index, and only call JSON.parse() once you hit content_block_stop. Parsing mid-stream will throw on incomplete JSON — this is the single most common bug in tool-use streaming code.

let buffers = {}; // keyed by block index

for await (const event of stream) {
  if (event.type === "content_block_start" && event.content_block.type === "tool_use") {
    buffers[event.index] = "";
  }
  if (event.type === "content_block_delta" && event.delta.type === "input_json_delta") {
    buffers[event.index] += event.delta.partial_json;
  }
  if (event.type === "content_block_stop" && buffers[event.index] !== undefined) {
    const input = JSON.parse(buffers[event.index]);
    // now safe to use
  }
}

For a full event-by-event reference, see /docs/streaming and /docs/tools.

Validate before you execute

Claude generates valid JSON almost all the time when you give it a well-formed tool schema, but "almost all" isn't good enough when the output drives a database write or an API call. Two defensive habits pay off:

A minimal zod check:

const schema = z.object({
  location: z.string().min(1),
  unit: z.enum(["celsius", "fahrenheit"]).default("celsius")
});

const parsed = schema.safeParse(call.input);
if (!parsed.success) {
  // return a tool_result with an error message so Claude can retry
}

If validation fails, don't just throw — return a tool_result block with is_error: true and a short message. Claude will usually self-correct the arguments on the next turn, which is more reliable than failing the whole request.

Common parsing mistakes to avoid

If you're building this on top of SubToAPI instead of calling the Claude API directly, the request and response shapes for tool use are identical — same tool_use blocks, same streaming deltas — so this parsing logic ports over with no changes. SubToAPI just wraps it behind your own sub_live_... key with usage metadata per request, which is handy if you're billing tool calls per customer. See /docs/tools for the exact request format and /docs/quickstart to get a key from /signup.

questions

Does Claude return tool arguments as a JSON string or a parsed object? In non-streaming responses, input is already a parsed JSON object. In streaming, it arrives as partial_json string fragments that you must concatenate and parse yourself once the content block closes.

What if Claude generates invalid JSON for a tool call? It's rare with a well-defined schema, but if validation fails, return a tool_result with is_error: true and a short description of what's wrong — Claude will typically retry with corrected input on the next turn.

How do I handle multiple tool calls in a single Claude response? Filter the content array for all blocks with type: "tool_use", execute each, and return one tool_result per call matched by tool_use_id — don't assume order is preserved or that there's only one.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →