Repository navigation
fix(py)!: a Tool is an Action; await tool(x) returns what the function returned - #6681
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors tool registration and invocation in the Genkit Python SDK so that directly awaiting a tool returns the underlying function's actual return type rather than a wrapped MultipartToolResponse envelope. This is achieved by introducing a generic Tool class, a new _ToolDecorator with overloads for type safety, and a direct_call context variable to intercept direct calls. Additionally, the test suite has been updated to reflect these changes. Feedback on the changes points out a typo in a type-ignore comment (# ty: ignore instead of # type: ignore) in the test suite.
huangjeff5
force-pushed
the
fix/py-tool-call-returns-value
branch
from
November 1, 2026 02:21
6347b07 to
bd09f7e
Compare
huangjeff5
force-pushed
the
fix/py-tool-call-returns-value
branch
from
November 2, 2026 12:43
bd09f7e to
01c6751
Compare
pavelgj
approved these changes
Nov 2, 2026
huangjeff5
force-pushed
the
fix/py-tool-call-returns-value
branch
from
November 2, 2026 23:05
01c6751 to
5e54343
Compare
huangjeff5
force-pushed
the
fix/py-tool-call-returns-value
branch
from
November 2, 2026 23:57
5e54343 to
6427a91
Compare
huangjeff5
force-pushed
the
fix/py-tool-call-returns-value
branch
from
November 3, 2026 00:46
6427a91 to
00d7a99
Compare
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.
A
Toolis now anAction.await tool(x)returns whatever your function returned, with zero framework wrapping. If your tool returns a string or a Pydantic model, you get back that string or model directly (instead of Genkit forcibly boxing it inside aMultipartToolResponsewire envelope). If your tool explicitly constructs and returns aMultipartToolResponse(to attach media/content parts for the model), you get back thatMultipartToolResponsewith your live output intact. This matches JS.Main Changes
ToolsubclassesActioninstead of wrapping one: Previously,Toolwrapped a privateActionbecauseregister_actiononly built plainActioninstances and forcibly adapted every tool function to return aMultipartToolResponseenvelope—so the innerActionand outerTooldisagreed on both output schema and return type. MakingToola realActionsubclass (likeFlow) drops that wrapper indirection while keepingToola distinct type sotools=[...]still rejects flows and models.Toolalways returns what the function returned (OutputT): Previously,await tool(x)returned a framework-constructedMultipartToolResponseenvelope withoutputpre-dumped to JSON. Now/tool/<name>returnsOutputTon every call path:PriceQuote, direct invocation yieldsPriceQuote.str, direct invocation yieldsstr.MultipartToolResponse[DishCheck](because it attaches media parts for the model), direct invocation yields thatMultipartToolResponse, but with the liveDishCheckinstance preserved on.outputrather than pre-dumped to a dictionary.Envelope conversion moves exclusively to wire boundaries: Inside
ai.generate, the tool execution loop runsTooland normalizesOutputTviaas_multipart_tool_responseonly when constructing theRole.TOOLPartfor the model turn (dump_tool_outputrunsmodel_dump(mode='json', by_alias=True), keeping snake_case fields as-is on standardBaseModels and honoring configured aliases). Return annotations written-> MultipartToolResponse[T]peelTfortool.definition().output_schemaso the model seesDishCheck's schema rather than the wire envelope. For the Dev UI and external HTTP callers, a separate/tool.v2/<name>registry entry returns the wire envelope—matching JS's dualtool/tool.v2registration.Direct calls still catch unserializable outputs early:
Tool._invokedry-runsas_multipart_tool_responsebefore returningOutputT. Returning a non-serializable object or an empty media part raisesINVALID_ARGUMENTimmediately in a unit test (await tool(x)), not only insideai.generate.Breaking changes
await tool(x)returns the function's value directly. Drop.outputon plain tool results (e.g.res = await quote(...)givesPriceQuote, notres.output).-> MultipartToolResponse[T]tools,res.outputis now the live typedTinstance instead of a pre-dumpeddict.Tool.action()is removed; pass theToolitself (it is anAction).ActionKind.TOOLis'tool'andActionKind.TOOL_V2is'tool.v2'. Tool spans insidegeneratenow report action typetoolinstead oftool.v2.-> MultipartToolResponse[T],tool.output_schemais the envelope schema; the schema bound by the model istool.definition().output_schema.Parity
defineToolregisterstool(raw output) andtool.v2(envelope), andawait tool(x)returns the raw output. Python now matches 1:1.tool.v2action under both keys, andRunRawreturns the unwrapped output.Landing notes
Import-line conflicts only (
_tools.pywith #6767,_registry.pywith #6947).