Tool calling and structured output
tools and response_format
Tool invocation
Let’s apply the functions that the model can call in tools. When the model calls a tool, tool_calls is executed. The results of this execution are then sent back as a message in the tool role.
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
answer = client.chat.completions.create(
model="anthropic/claude-opus-5.5",
messages=[{"role": "user", "content": "What is the weather in Seoul??"}],
tools=tools,
)
call = answer.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)- The models that receive the tools can be seen in Support for each model. If you try to send them to the models that do not receive the tools, a
tools_not_supportederror will occur. - Use
tool_choiceto require a tool call (required) or prevent it (none). Models that cannot require tool calls return aforced_tool_not_supportederror.
Received in a predetermined format
When a JSON schema is given to response_format, it will be returned in that exact format.
answer = client.chat.completions.create(
model="anthropic/claude-opus-5.5",
messages=[{"role": "user", "content": "What are the populations of Seoul and Busan?"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "cities",
"schema": {
"type": "object",
"properties": {
"cities": {"type": "array", "items": {
"type": "object",
"properties": {"name": {"type": "string"}, "population": {"type": "integer"}},
"required": ["name", "population"],
}},
},
"required": ["cities"],
},
},
},
)If the format that the model accepts is not provided, it will be reported as an error: response_format_not_supported. In such cases, please change the format that the model accepts, or simply specify the desired format in the prompt.