Skip to content

fix(py): support string tools across providers - #6907

Closed
FOWEPJF255 wants to merge 5 commits into
genkit-ai:mainfrom
FOWEPJF255:FOWEPJF255/fix-python-string-tools
Closed

FOWEPJF255 wants to merge 5 commits into
genkit-ai:mainfrom
FOWEPJF255:FOWEPJF255/fix-python-string-tools

Conversation

@FOWEPJF255

Copy link
Copy Markdown
Contributor

Fixes genkit-ai/genkit-python#214

Summary

  • Wrap bare-string tool schemas in an { "input": ... } object for Anthropic, OpenAI, and Bedrock wire APIs, then unwrap their tool calls before Genkit dispatch.
  • Apply the same envelope to prior tool calls in conversation history, without changing object-shaped tools.
  • Keep streamed string tool calls consistent with their final bare-string input.

Validation

  • Three provider test suites: 1,215 passed, 29 skipped, 1 deselected. The deselected Anthropic SDK initialization test fails on this Windows environment when it clears all environment variables and cannot determine the home directory; it does not exercise tool input handling.
  • Added a mock OpenAI provider to real Genkit tool-dispatch test, plus provider schema, response, streaming, and history regressions.
  • Pyright on changed implementation and OpenAI regression tests: 0 errors. Ruff lint and changed-file formatting checks passed. git diff --check passed.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +620 to +622
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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +41 to +45
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 ()
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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 ()
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

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.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

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

@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(py): string tools still fail on Anthropic, OpenAI, and Bedrock

2 participants