ValueError
anthropic.client.exceptions.ValueError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.messages.create(
File "/usr/local/lib/python3.9/site-packages/anthropic/client.py", line 210, in create
raise ValueError('Invalid input_schema: must be a valid JSON schema dict')
ValueError: Invalid input_schema: must be a valid JSON schema dict Why it happens
Anthropic's function-calling feature requires the input_schema parameter to be a valid JSON schema dictionary describing the expected input structure. If the schema is malformed, missing required fields, or not a dict, the client raises this ValueError to prevent sending invalid tool use calls.
Detection
Validate your input_schema against JSON schema standards before calling client.messages.create; log or assert the schema structure to catch errors early.
Causes & fixes
input_schema is not a dictionary but a string or other type
Ensure input_schema is a Python dict representing the JSON schema, not a JSON string or other type.
input_schema missing required JSON schema keys like 'type' or 'properties'
Add required keys such as 'type': 'object' and 'properties' with field definitions to the input_schema dict.
input_schema fields have invalid types or unsupported schema constructs
Use only supported JSON schema types and constructs as per Anthropic's documentation; validate schema with a JSON schema validator.
Passing input_schema as None or empty dict
Provide a fully defined JSON schema dict describing the expected input fields instead of None or {}.
Code: broken vs fixed
from anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Broken: input_schema is a JSON string, not dict
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Call tool with data."}],
tool_use={
"name": "my_tool",
"input_schema": '{"type": "object", "properties": {"query": {"type": "string"}}}' # invalid
}
) # This line raises ValueError from anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Fixed: input_schema is a dict, not a string
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Call tool with data."}],
tool_use={
"name": "my_tool",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string"}
},
"required": ["query"]
} # Changed to dict with required keys
}
)
print(response) # Shows successful response Workaround
Catch the ValueError exception around client.messages.create, then manually validate or fix the input_schema before retrying the call.
Prevention
Always define and validate your input_schema as a proper JSON schema dict before passing it to Anthropic's tool_use parameter to avoid runtime validation errors.