How do I parse tool call responses?
Understand the Response Format
When the model decides to call a tool, the API returns a message with a tool_calls array (OpenAI) or content blocks of type tool_use (Anthropic). Each call includes an ID, the function name, and arguments. The arguments are typically a JSON string that you need to parse.
For example, OpenAI returns: { "id": "call_abc", "type": "function", "function": { "name": "get_weather", "arguments": "{\"location\":\"Boston\"}" } }. You parse the arguments string into an object.
- Check for tool_calls in the response message
- Loop through each tool call
- Extract function.name and function.arguments
- Parse arguments with JSON.parse (or equivalent)
- Execute your function with the parsed arguments
Handle Errors and Validation
Arguments may be malformed or missing required fields. Always validate against your tool's schema before executing. If parsing fails, return an error message to the model so it can retry or correct itself.
Also, handle cases where the model calls a non-existent tool. Maintain a mapping of tool names to functions and check if the name exists.
Return Results to the Model
After executing the tool, you must send the result back to the model in a follow-up request. Include the tool_call_id and the output as a string (often JSON). The model will then generate a final response incorporating the tool result.
For OpenAI, you add a message with role 'tool', tool_call_id, and content. For Anthropic, you add a tool_result content block. This completes the loop.
Common mistakes
- Forgetting to parse the arguments string as JSON, leading to errors when accessing properties.
- Not validating arguments against the schema, causing runtime errors in your function.
- Failing to send the tool result back to the model, leaving the conversation incomplete.