genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] compat-oai: Responses transport drops built-in tool-call items - breaks stateless multi-turn and discards image generation output

Open cabljac opened this issue 3 months ago • 1 comments

Part of #6002. Applies to the Responses transport landing in #6006 / #6148.

The Responses transport converts only reasoning, message, and function_call output items into Genkit parts. Built-in tool-call items (web_search_call, code_interpreter_call, image_generation_call, mcp_call, mcp_approval_request, ...) fall through the conversion loop with no part emitted, which causes two related failures:

1. Stateless multi-turn replay breaks with built-in tools (major). The transport pins store: false and replays the full history each turn. OpenAI's conversation-state rules for stateless reasoning-model requests require every item in the response's output array to be preserved on replay, and the API enforces reasoning/tool-item pairing in both directions (the Item 'rs_...' of type 'reasoning' was provided without its required following item 400 family). Since built-in tool items are never converted to parts, they can never be replayed: a reasoning model with config: { tools: [{ type: 'web_search' }] } (an explicitly supported passthrough) emits [reasoning, web_search_call, message], and every turn after the first replays [reasoning(id, encrypted), assistant-text] with the web_search_call missing -> 400.

2. image_generation_call output is silently discarded (major). The conversion loop's assumption that "the caller-visible result arrives in the message item alongside them" does not hold for image generation: the generated image lives in the item's own result field and there is no message item carrying it. The model generates the image, the call is billed, and the plugin returns a message with no media part and no error. mcp_approval_request items are likewise dropped, dead-ending MCP approval flows.

Fix direction:

  • Round-trip built-in tool-call items through part metadata the same way reasoning items already ride metadata.itemId/metadata.encryptedContent, re-emitting them verbatim from history conversion - or reject built-in tools in config until replay is supported, so the failure is loud instead of a confusing 400 on turn two.
  • Map image_generation_call.result to a media part, and handle the remaining skipped item types deliberately per type.

Found by adversarial review of the combined #6006 + #6148 diff; single-turn built-in tool use (citations via metadata.annotations) works and is covered by tests - the gap is multi-turn replay and item types whose results are not mirrored into a message item.

cabljac avatar Aug 24 '26 15:08 cabljac

Triage: P2 · M · stop: needs design decision

Area: js

Root cause: The OpenAI Responses transport referenced by this issue (#6006 and #6148) has not landed on main yet (js/plugins/compat-oai/src/model.ts currently only implements the Chat Completions API), and handling built-in tool-call output items requires a contract decision on Genkit Part and metadata representation.

Options:

  1. Round-trip built-in tool items via Part and metadata (recommended once #6006/#6148 land): Map image_generation_call to a media part (data:image/...;base64,...) with item metadata for replay, represent mcp_approval_request as a structured custom or toolRequest part, and persist intermediate built-in tool items (web_search_call, code_interpreter_call, mcp_call) in Part.metadata so stateless history conversion re-emits them in order.
  2. Reject unsupported built-in tools in config: Throw INVALID_ARGUMENT when config.tools includes built-in tools that cannot be replayed statelessly or mapped to Genkit parts.
Details
  • Checked js/plugins/compat-oai/src/model.ts, js/plugins/compat-oai/src/index.ts, and js/plugins/compat-oai/src/openai/index.ts at commit 9a7d0b6: only client.chat.completions.create and client.beta.chat.completions.stream exist on main.
  • No Responses API transport (client.responses), metadata.itemId, or metadata.encryptedContent handling is present in js/plugins/compat-oai yet.
  • Once the Responses transport lands on main, Option 1 preserves stateless multi-turn compatibility with reasoning models (store: false) and surfaces image_generation_call.result media output without breaking single-turn built-in tool usage.

Run a8de04ca-188d-4e19-8c82-afe57246de28 · Cloud Run logs

genkit-bot avatar Nov 20 '26 15:11 genkit-bot