Repository navigation
fix(py): support string tools across providers - #6907
FOWEPJF255 wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to wrap and unwrap bare string tool inputs into object-based schemas (using a wire envelope) to support provider APIs (Amazon Bedrock, Anthropic, and OpenAI) that only accept object schemas. It adds helper functions in a new _tool_input.py module and updates the respective model converters and tests. The review feedback highlights two opportunities for defensive programming: safely checking if complete_call.function is not None before accessing its name during OpenAI streaming, and ensuring elements in the tools list are not None within is_string_tool to prevent potential AttributeErrors.
| complete_call = tool_calls[tool_call.index] | ||
| if not is_string_tool(complete_call.function.name, request.tools): | ||
| parts.append(MessageConverter.tool_call_to_genkit(complete_call, args_segment=fragment)) |
There was a problem hiding this comment.
Defensive programming: complete_call.function can be None during streaming (as checked on line 618). Accessing complete_call.function.name directly without a check can raise an AttributeError. We should safely retrieve the function name before checking if it is a string tool.
| complete_call = tool_calls[tool_call.index] | |
| if not is_string_tool(complete_call.function.name, request.tools): | |
| parts.append(MessageConverter.tool_call_to_genkit(complete_call, args_segment=fragment)) | |
| complete_call = tool_calls[tool_call.index] | |
| tool_name = complete_call.function.name if complete_call.function else None | |
| if not (tool_name and is_string_tool(tool_name, request.tools)): | |
| parts.append(MessageConverter.tool_call_to_genkit(complete_call, args_segment=fragment)) |
There was a problem hiding this comment.
The stream path now checks complete_call.function and its name before accessing the tool name, then waits for later chunks when function metadata has not arrived yet. The test_generate_stream_accepts_tool_call_before_function regression covers this ordering and passes on the updated head 9b17224.
| def is_string_tool(name: str, tools: Sequence[ToolDefinition] | None) -> bool: | ||
| return any( | ||
| tool.name == name and isinstance(tool.input_schema, dict) and tool.input_schema.get('type') == 'string' | ||
| for tool in tools or () | ||
| ) |
There was a problem hiding this comment.
Defensive programming: tools list might contain None elements (as seen in other parts of the codebase where tool is not None is checked). We should add a safety check to prevent AttributeError if any element in tools is None.
| def is_string_tool(name: str, tools: Sequence[ToolDefinition] | None) -> bool: | |
| return any( | |
| tool.name == name and isinstance(tool.input_schema, dict) and tool.input_schema.get('type') == 'string' | |
| for tool in tools or () | |
| ) | |
| def is_string_tool(name: str, tools: Sequence[ToolDefinition] | None) -> bool: | |
| return any( | |
| tool is not None and tool.name == name and isinstance(tool.input_schema, dict) and tool.input_schema.get('type') == 'string' | |
| for tool in tools or () | |
| ) |
There was a problem hiding this comment.
Fixed the nullable tools-list case in 5254f4f. is_string_tool now skips None entries, and the new regression test fails on the prior head with AttributeError and passes after the change. The streamed function=None case is already guarded in the preceding commit (0f66b1b). The focused tests, Ruff, Pyrefly, and commit diff check pass.
|
Merged current main into the existing string-tool PR at 9b17224 and resolved the provider API conflicts. The Anthropic, Bedrock, and OpenAI suites pass with 1,256 tests passed, 29 live tests skipped, and one Windows-only environment test deselected because clearing all environment variables prevents pathlib from locating the home directory. Pyright reports 0 errors on the changed implementation and dispatch test; Ruff lint, the changed test formatting check, uv lock --check, and git diff --check pass. CI is running on the updated head. |
|
Hi @FOWEPJF255 — heads up that Genkit Python SDK development has moved to https://github.com/genkit-ai/genkit-python! Could you re-open this PR over there (file paths map 1:1 after stripping the leading py/ prefix), and link/close this PR once moved? cc @huangjeff5 |
|
Migrated the Bedrock portion to genkit-ai/genkit-python#167, with both regression cases reproduced before the fix and 586 offline Bedrock tests passing afterward. OpenAI string-tool handling is already covered by genkit-ai/genkit-python#87, and Anthropic is being handled in genkit-ai/genkit-python#111, so the new PR is intentionally scoped to Bedrock. Closing this legacy PR as requested. |
Fixes genkit-ai/genkit-python#214
Summary
{ "input": ... }object for Anthropic, OpenAI, and Bedrock wire APIs, then unwrap their tool calls before Genkit dispatch.Validation
git diff --checkpassed.No API documentation changes; this corrects the existing string tool contract.
AI assistance was used to prepare the implementation and tests; the changes were validated with the checks above.