Skip to content

refactor(google-genai)!: own Interactions API types; built-in tool round-trips - #105

Open
ifielker wants to merge 1 commit into
if-pyInteractionFixesfrom
if-pyTypes
Open

ifielker wants to merge 1 commit into
if-pyInteractionFixesfrom
if-pyTypes

Conversation

@ifielker

Copy link
Copy Markdown
Contributor

refactor(py/google-genai)!: own Interactions API types; built-in tool steps round-trip like JS

The Interactions API code (used by Antigravity, Lyria and Deep Research) now
uses the plugin's own wire types instead of the google-genai SDK's generated
google.genai.interactions types, like the JS and Dart plugins do. This
prepares that code for Gemini models, which move to the Interactions API in
a follow-up PR. Gemini, Gemma, Imagen, Veo and embedder models are
unchanged, and so is the HTTP client (it was already the plugin's own).

Why

The SDK's generated types didn't fit how the plugin uses the API:

  • Parsing a response filled in fields the server didn't send. A
    url_context_call step with only an id came back with arguments: {},
    an mcp_server_tool_result with result: {}, other steps with
    result: None. Those fields were then saved in the message history and
    sent back to the server on the next turn.
  • The request types were out of date or too strict (e.g. a code execution
    language of 'python' only, while the API also takes 'bash'; required
    fields the server itself leaves out), so the plugin needed casts and a
    private SDK import to build valid requests.

The new types

_interactions/_types.py mirrors the JS plugin's interaction-types.ts
(same type names, same order). Each type is a TypedDict of the wire JSON:

  • Responses are validated with pydantic and keep exactly what the server
    sent: nothing is filled in, and fields the types don't list are kept and
    sent back unchanged.
  • Only type is required. The server leaves out fields its spec marks as
    required at times, and one missing field shouldn't fail a whole response.
  • A step, content block or annotation the plugin doesn't know (or whose
    fields don't match its type) is kept as sent, as UnknownStep,
    UnknownContent, ...
  • The request side covers what's sent today; the request config types
    come with the Gemini request mapping.

The stream event types are included too; they're used by the streaming
client in the follow-up PR.

Built-in tool steps

Steps for the server-side tools (URL context, file search, Google Maps, MCP
servers) and agentic video processing steps now come back as custom parts
with the JS plugin's names, and are sent back as the same steps (with their
signatures) when the history is resent:

  • urlContextCall / urlContextResult, fileSearchCall /
    fileSearchResult, googleMapsCall / googleMapsResult,
    mcpServerToolCall / mcpServerToolResult,
    processingCall / processingResult.
  • Fields the server left out are left out of the part and of the resent
    step.

Before, these came back as unknownStep parts, which held the SDK's filled-in
fields. History saved with unknownStep parts is still sent back; None
values in those parts (fields the SDK added) are dropped first.

Google Search parts now also keep the step's search_type (as searchType)
and is_error (as isError), and leave out arguments / result when the
server didn't send them, as in JS.

Two smaller fixes in the same spirit (steps go back as they came):

  • Function results (e.g. Antigravity's sandbox tools): is_error: false
    is now sent back as false; only true used to be sent.
  • Thought steps without a signature: the server sometimes sends the
    thought summary and the signature as separate steps. A step without a
    signature no longer gets metadata.thoughtSignature: None on its part.

raw only

Responses now carry the interaction in raw only, exactly as the server
sent it. The response-level custom field (deprecated by Genkit in favor of
raw) no longer holds a copy.

Breaking changes

These only affect code that reads Interactions API responses at a low level:

  • Built-in tool step parts: URL context, file search, Google Maps, MCP
    server and processing steps are custom parts with their own key (see
    above) instead of custom['unknownStep'].
  • response.custom: no longer holds the interaction; read
    response.raw.
  • response.raw: holds the interaction as the server sent it, without
    the fields the SDK used to fill in (None values, empty arguments).

Testing

  • Unit tests: new tests for the types (nothing filled in, unknown fields
    kept, unknown steps / content / annotations / stream deltas / events kept
    as sent); ported from the JS plugin's tests with the same data: built-in
    tool steps (Google Maps and URL context shapes seen live, file search and
    MCP from the spec; converted to parts with their signatures, never tool
    requests, sent back unchanged, ids-only steps round-trip, Google Search
    steps without arguments / result, an empty trailing model_output
    step) and agentic video processing steps; saved unknownStep parts sent
    back without their None values; raw is the interaction as received;
    function results resend is_error: false; thought steps without a
    signature get no signature metadata.
    The existing converter, client and model tests run against the new types.
  • Live (Gemini API): an Antigravity call with the url_context tool.
    The response had four URL context call/result pairs (with signatures),
    thought steps and sandbox function calls. The URL context steps came back
    as urlContextCall / urlContextResult parts (none as unknownStep),
    and converting the response back into history gave the server's steps
    exactly as received: no empty arguments, no null fields. raw held
    the response as sent, including fields the types don't list
    (service_tier, environment, tools).
  • ruff format --check, ruff check, pyright, ty, pyrefly and the
    package's unit tests (1582) pass.

@ifielker
ifielker added this pull request to stack #106 November 11, 2026 20:13
return PlainValidator(lambda value: as_unknown(cls, key, value))


def keep_unknown(cls: type[UnknownObject], key: str) -> WrapValidator:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The module docstring says one missing or off-shape field shouldn't fail a whole response, but a JSON null still does. I haven't seen the server send one, but if it does: a top-level or usage / errors null raises INTERNAL "Unable to parse Interaction response", and a null on a step demotes it to UnknownStep. For a pending function_call with signature: null, the turn ends stop with no tool request (ran it).

Maybe we can drop None values before validating, so null acts like a missing key.

call_id: NotRequired[str]
is_error: NotRequired[bool]
# JS: text and image content only in the list. Any content is accepted.
result: NotRequired[dict[str, object] | str | list[Content]]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is a sandbox function_result.result ever a JSON list (e.g. [{'path': 'a.py'}])? If so it doesn't match this union, the step becomes UnknownStep, and get_pending_function_call_ids reports the already-run sandbox call as pending, so Genkit would try to run it locally.

'arguments': call.get('arguments'),
}
signature = signature_of(metadata)
if (search := read_as(GOOGLE_SEARCH_CALL_PART, custom.get(GOOGLE_SEARCH_CALL))) is not None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

History saved by the current plugin can have None values in googleSearchCall.arguments, googleSearchResult.callId and executableCode.code. read_as rejects those, so the part falls through to "Unsupported part type" and is dropped: the resent history keeps the result but loses the call and its signature. without_none only runs on unknownStep parts.

I suggest running it before every read_as here.

How the JS types map to Python:

- Each type is a TypedDict: the wire JSON itself. The plugin builds them as
dict literals and sends them as they are. ``extra_items=object`` keeps

@huangjeff5 huangjeff5 Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

TypedDict(..., extra_items=object) needs typing_extensions>=4.13 (import fails on 4.12) and pydantic>=2.12 (2.11 silently drops the unlisted fields like service_tier / environment / tools from raw), but pyproject.toml still says pydantic>=2.11 and typing_extensions>=4.2. It works today only because google-genai pulls in newer versions.

I suggest bumping the two floors to pydantic>=2.12 and typing_extensions>=4.14.1 (pydantic 2.12's own floor, which covers the 4.13 that extra_items needs).

payload: dict[str, object] = {
'name': step.get('name', ''),
'result': step.get('result'),
'call_id': step.get('call_id'),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

serverFunctionResult is the one custom part still writing call_id / is_error; every other part next to it is callId / isError, and so is the JS plugin's serverFunctionResult. Once Gemini moves onto this path these are the keys everyone reads off part.custom.

I suggest writing the camelCase keys. The reader already takes both, so old history keeps working.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants