Skip to content

fix(py/anthropic): send tools with non-object inputs under an input field - #111

Open
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-anthropic-scalar-tool-input
Open

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-anthropic-scalar-tool-input

Conversation

@huangjeff5

Copy link
Copy Markdown
Collaborator

Claude tools whose input is a plain value instead of an object now work. Before, any tool like get_weather(city: str) or one taking list[str] made every request fail with an Anthropic 400, even if Claude never called the tool.

@ai.tool()
async def get_weather(city: str) -> str:
    return f'Sunny in {city}'


response = await ai.generate(
    model='anthropic/claude-sonnet-4-5',
    prompt='What is the weather in Paris?',
    tools=[get_weather],
)
# before
# GenkitError: 400 tools.0.custom.input_schema.type: Input should be 'object'
# after
# get_weather runs with 'Paris'; response.text is Claude's answer.

Decisions

  • Non-object tool inputs travel as {"input": ...}: Previously, a tool schema like {"type": "string"} was sent to Claude as-is and the API rejected the whole request. Now it is declared as {"type": "object", "properties": {"input": <schema>}, "required": ["input"]}, and your tool still runs with the bare value ('Paris', not {'input': 'Paris'}) in both generate and generate_stream. We wrap rather than reject at define time because the tool definition is valid Genkit and works with other models; only the Anthropic wire shape needs an object.
  • Optional inputs stay optional: A tool whose input accepts None (e.g. city: str | None) is wrapped without required, so Claude can call it with no arguments and the tool gets None. Marking it required would force Claude to invent a value the tool doesn't need.
  • History goes back in the shape the tool was declared with: Earlier calls to a wrapped tool are re-sent as {"input": ...} so multi-turn conversations stay consistent with the schema Claude sees. A past call to a tool that isn't in the current request's tools is sent back unchanged, since there's no declared schema to match it against.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant