Skip to content

fix(js/plugins/compat-oai): send multipart tool results to the model - #7212

Open
abhiramshaji-cyber wants to merge 5 commits into
genkit-ai:mainfrom
abhiramshaji-cyber:fix/compat-oai-tool-response-content
Open

abhiramshaji-cyber wants to merge 5 commits into
genkit-ai:mainfrom
abhiramshaji-cyber:fix/compat-oai-tool-response-content

Conversation

@abhiramshaji-cyber

@abhiramshaji-cyber abhiramshaji-cyber commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

This PR makes text returned by tools reach OpenAI compatible models, and stops image only tool results from sending a message the API rejects.

What this code is for

toOpenAIMessages in js/plugins/compat-oai/src/model.ts turns Genkit messages into Chat Completions messages for every OpenAI compatible model (OpenAI, xAI, DeepSeek, custom ones). toOpenAIRequestBody calls it on every request. Its case 'tool' branch writes one { role: 'tool', tool_call_id, content } message per tool response, and Chat Completions only accepts text in that content.

toOpenAIMessages call graph

toOpenAIMessages with its one caller and its two callees, before this PR.

Before this PR

A model calls an MCP screenshot tool and a lookup tool in one turn.

  1. screenshot returns content: [{ media }] with output undefined; lookup returns output: { id: 7 } plus content: [{ text: 'extra notes' }].
  2. case 'tool' sets content to JSON.stringify(toolResponse.output) and never reads toolResponse.content.
  3. lookup loses extra notes, and screenshot goes out as {"role":"tool","tool_call_id":"call_1"} with no content, which OpenAI rejects.

After this PR

Same turn. case 'tool' calls the new toOpenAIToolResult, so lookup sends {"id":7}\n\nextra notes, and screenshot always gets a string content. On a model with supports.media the image follows in one user message after the tool messages; on any other model the tool message says Media (image/png) omitted.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: tool message sent with no content"]
direction TB
  B1["screenshot tool returns only media"] --> B2["toOpenAIMessages case tool"]
  B2 --> B3["reads toolResponse.output only"]
  B3 --> B4{"output defined?"}
  B4 -->|"undefined, content ignored"| B5["role tool, no content, OpenAI rejects"]
  B4 -->|"defined"| B6["content is JSON of output"]
end

subgraph AFTER["AFTER: every tool message has content"]
direction TB
  A1["screenshot tool returns only media"] --> A2["toOpenAIMessages case tool"]
  A2 --> A3["toOpenAIToolResult reads output and content"]
  A3 --> A4{"THE CHANGE: text stays, media checked"}
  A4 -->|"image on a supports.media model"| A5["tool message, then user message with image_url"]
  A4 -->|"output only"| A6["content is JSON of output, same as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

The fix is the new toOpenAIToolResult. The tool message content is the output (a string as is, anything else as JSON), then a blank line, then the parts from toolResponse.content:

  • text parts, joined as they are
  • each resource on its own line as Resource (<uri>)
  • each data part on its own line as JSON.stringify(part.data)
  • custom, reasoning and other part types skipped, same as today
  • media that is not forwarded on its own line as Media (<contentType>) omitted

Media is forwarded only when the model declares supports.media and the part is an image or a base64 PDF, the two things Chat Completions accepts in a user message. defineCompatOpenAIModel passes modelRef.info.supports.media through openAIModelRunner and toOpenAIRequestBody into toOpenAIMessages as a new optional supportsMedia argument, which defaults to false. So DeepSeek and the text only Grok models never get image_url, and audio, other blobs and remote URLs that are not images never throw.

When there is no output and no text, content is Media from tool <name> (<ref>) follows in the next user message. if media was forwarded, otherwise the string "null". That is what JSON.stringify(null) gives today for a null output; for an undefined output today there is no content at all.

To keep the order valid, toOpenAIMessages collects forwarded media in toolMedia and only pushes the user message (flushToolMedia) when the next message is not a tool message, or at the end, so all tool messages of a turn stay together even across two Genkit tool messages.

Tests in compat_oai_test.ts cover text only, output plus text and a resource, a standalone resource, a data part next to a custom one, empty text, nothing at all, media only, output plus text plus a PDF, several tool responses across two tool messages, and the omitted cases: an image on a model without media support, audio, a JSON blob, and a remote PDF URL. One toOpenAIRequestBody test checks the supportsMedia flag. The existing output only cases are unchanged.

What changes for a user

Fixes #7211

The tool branch of toOpenAIMessages only read toolResponse.output, so text
parts in a multipart tool response never reached the model, and a media only
result (output undefined) produced a tool message with no content, which
OpenAI rejects. Tool messages now carry the output plus content text, and
media goes in one user message after the turn's tool messages so they stay
contiguous.

Fixes genkit-ai#7211

@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 processing and formatting tool response content and media within the OpenAI compatibility plugin, including flushing media parts as subsequent user messages and adding comprehensive tests. The reviewer feedback highlights three key areas for improvement: safely ignoring unsupported part types in tool responses to prevent runtime crashes, merging flushed tool media into subsequent user messages to comply with strict alternating role requirements of certain LLM providers, and using an empty string instead of the literal 'null' as a fallback placeholder to avoid model confusion.

Comment thread js/plugins/compat-oai/src/model.ts Resolved
Comment thread js/plugins/compat-oai/src/model.ts Resolved
Comment thread js/plugins/compat-oai/src/model.ts Resolved
…ontent

Requests that send today must keep sending. Tool response media is now only
forwarded when the model declares supports.media (passed down from
defineCompatOpenAIModel), and only as images or base64 PDFs, which Chat
Completions accepts in a user message. Anything else becomes a
"Media (<type>) omitted" line in the tool message instead of throwing, so
DeepSeek and text only Grok never receive image_url.

Data parts are sent as JSON text, other part types are skipped as before,
and each resource sits on its own line.
Resource, data and omitted media lines now appear where their parts sit
instead of after all text, with adjacent text parts still joined. A test
through defineCompatOpenAIModel pins that a model declaring media: false
never receives tool response images.
…-response-content

# Conflicts:
#	js/plugins/compat-oai/src/model.ts
…-response-content

# Conflicts:
#	js/plugins/compat-oai/src/model.ts

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JS] compat-oai drops multipart tool response content; media only tool result sends a tool message with no content

1 participant