Repository navigation
fix(py): allow tools with only a ToolRunContext parameter - #6166
Closed
Evhye38496 wants to merge 2 commits into
Closed
Evhye38496 wants to merge 2 commits into
Evhye38496 wants to merge 2 commits into
Conversation
A tool whose sole parameter is typed ToolRunContext crashed at definition time with PydanticSchemaGenerationError, because the schema inference treated the context parameter as the tool's input type and fed it to TypeAdapter(). Even if that were bypassed, the runtime dispatch passed the raw tool input to the context parameter instead of constructing a ToolRunContext. Skip ActionRunContext-typed parameters when inferring input schemas (action input schema becomes empty, matching zero-arg tools), and teach both the Action dispatch and the tool wrapper to pass a properly constructed ToolRunContext when the sole parameter is context-only. ISSUE: genkit-ai#4492 CHANGELOG: - [x] Fix schema inference for context-only tool parameters - [x] Fix runtime dispatch for context-only tool parameters - [x] Add unit tests (ctx-only, input+ctx, input-only, zero-arg)
Contributor
There was a problem hiding this comment.
Code Review
This pull request adds support for tools that only accept a context parameter (such as ToolRunContext) without an input parameter, resolving schema generation errors. It introduces helper logic to identify context annotations and updates argument dispatching. The review feedback suggests improving _is_context_annotation to handle string annotations when from __future__ import annotations is used, fixing a potential IndexError in _invoke when a context-only parameter has a default value, and adding a corresponding test case.
Address review feedback on genkit-ai#6166: - _is_context_annotation now also recognizes string annotations ("ToolRunContext") for modules using from __future__ import annotations, where get_type_hints may fail and raw string annotations are used. - Action._invoke checks for a context-typed sole parameter before omit_input, so a context parameter with a Python default no longer results in a zero-arg call and an IndexError in the tool wrapper. - Add tests: string annotation recognition, and a context-only tool whose parameter has a default invoked without input.
Contributor
|
Closing as superseded: Python SDK development has moved to https://github.com/genkit-ai/genkit-python, where context-only tool typing is addressed in genkit-ai/genkit-python#46. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #4492 — a tool whose sole parameter is typed
ToolRunContextpreviously crashed at definition time withPydanticSchemaGenerationError, and would have received the raw tool input instead of a context at runtime.Changes
_core/_action.py_is_context_annotation()helper:Truewhen an annotation isActionRunContextor a subclass (e.g.ToolRunContext)._initialize_io_schemasnow skipsActionRunContext-typed parameters when inferring the input schema — a context-only parameter is no longer fed toTypeAdapter(...). Context-only tools expose an empty input schema, matching zero-argument tools.Action._invoke(1-arg dispatch) passes the run context rather than the input when the sole parameter is context-typed._ai/_tools.pyToolRunContextparameter it constructs a properToolRunContext(with resume metadata / original input) before calling the tool.(input, ctx)and single-input tools are unchanged.Repro
Before:
PydanticSchemaGenerationErrorat definition time. After: defines and runs, receiving the context.Tests
tests/genkit/ai/_tools_test.py: added 4 testsToolRunContextwith the correct context;(input, ctx)tools;_tools_test.py: 21 passed.tests/genkit/ai+tests/genkit/core: 847 passed. (Two collection errors are pre-existing and unrelated —tests/specs/*.yamlis missing from the repo;already failing on main.)ruff format --preview,ruff check --preview --unsafe-fixes, andty checkall pass on the changed files.Docs
N/A — internal behavior fix; API reference pages under
py/docsare autogenerated.