Skip to content

fix(py): allow tools with only a ToolRunContext parameter - #6166

Closed
Evhye38496 wants to merge 2 commits into
genkit-ai:mainfrom
Evhye38496:evhye38496/fix-tool-context-only
Closed

Evhye38496 wants to merge 2 commits into
genkit-ai:mainfrom
Evhye38496:evhye38496/fix-tool-context-only

Conversation

@Evhye38496

Copy link
Copy Markdown

What

Fixes #4492 — a tool whose sole parameter is typed ToolRunContext previously crashed at definition time with PydanticSchemaGenerationError, and would have received the raw tool input instead of a context at runtime.

Changes

  • _core/_action.py
    • New _is_context_annotation() helper: True when an annotation is ActionRunContext or a subclass (e.g. ToolRunContext).
    • _initialize_io_schemas now skips ActionRunContext-typed parameters when inferring the input schema — a context-only parameter is no longer fed to TypeAdapter(...). 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.py
    • The tool wrapper resolves the user function's annotations and dispatches on them: for a sole ToolRunContext parameter it constructs a proper ToolRunContext (with resume metadata / original input) before calling the tool.
    • (input, ctx) and single-input tools are unchanged.

Repro

from genkit import Genkit, ToolRunContext

ai = Genkit()

@ai.tool()
async def get_user_data(ctx: ToolRunContext) -> str:
    raw_user = ctx.context.get('user')
    ...

Before: PydanticSchemaGenerationError at definition time. After: defines and runs, receiving the context.

Tests

  • tests/genkit/ai/_tools_test.py: added 4 tests
    • context-only tool defines with empty input schema and receives a ToolRunContext with the correct context;
    • restart flow on a context-only tool surfaces resume metadata;
    • regression: (input, ctx) tools;
    • regression: single-input tools.
  • _tools_test.py: 21 passed.
  • tests/genkit/ai + tests/genkit/core: 847 passed. (Two collection errors are pre-existing and unrelated — tests/specs/*.yaml is missing from the repo; already failing on main.)
  • Lint: ruff format --preview, ruff check --preview --unsafe-fixes, and ty check all pass on the changed files.

Docs

N/A — internal behavior fix; API reference pages under py/docs are autogenerated.

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)

@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 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.
@MichaelDoyle

Copy link
Copy Markdown
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.

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.

Python SDK: Tools with only ToolRunContext parameter crash with PydanticSchemaGenerationError

2 participants