Repository navigation
fix(js/plugins/compat-oai): send multipart tool results to the model - #7212
Open
abhiramshaji-cyber wants to merge 5 commits into
Open
abhiramshaji-cyber wants to merge 5 commits into
abhiramshaji-cyber wants to merge 5 commits into
Conversation
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
Contributor
There was a problem hiding this comment.
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.
…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.
3 of 4 tasks
…-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
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.
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
toOpenAIMessagesinjs/plugins/compat-oai/src/model.tsturns Genkit messages into Chat Completions messages for every OpenAI compatible model (OpenAI, xAI, DeepSeek, custom ones).toOpenAIRequestBodycalls it on every request. Itscase 'tool'branch writes one{ role: 'tool', tool_call_id, content }message per tool response, and Chat Completions only accepts text in thatcontent.toOpenAIMessageswith its one caller and its two callees, before this PR.Before this PR
A model calls an MCP
screenshottool and alookuptool in one turn.screenshotreturnscontent: [{ media }]withoutputundefined;lookupreturnsoutput: { id: 7 }pluscontent: [{ text: 'extra notes' }].case 'tool'setscontenttoJSON.stringify(toolResponse.output)and never readstoolResponse.content.lookuplosesextra notes, andscreenshotgoes out as{"role":"tool","tool_call_id":"call_1"}with nocontent, which OpenAI rejects.After this PR
Same turn.
case 'tool'calls the newtoOpenAIToolResult, solookupsends{"id":7}\n\nextra notes, andscreenshotalways gets a stringcontent. On a model withsupports.mediathe image follows in one user message after the tool messages; on any other model the tool message saysMedia (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:#111827The change
The fix is the new
toOpenAIToolResult. The tool messagecontentis the output (a string as is, anything else as JSON), then a blank line, then the parts fromtoolResponse.content:resourceon its own line asResource (<uri>)datapart on its own line asJSON.stringify(part.data)custom,reasoningand other part types skipped, same as todayMedia (<contentType>) omittedMedia is forwarded only when the model declares
supports.mediaand the part is an image or a base64 PDF, the two things Chat Completions accepts in a user message.defineCompatOpenAIModelpassesmodelRef.info.supports.mediathroughopenAIModelRunnerandtoOpenAIRequestBodyintotoOpenAIMessagesas a new optionalsupportsMediaargument, which defaults tofalse. So DeepSeek and the text only Grok models never getimage_url, and audio, other blobs and remote URLs that are not images never throw.When there is no output and no text,
contentisMedia from tool <name> (<ref>) follows in the next user message.if media was forwarded, otherwise the string"null". That is whatJSON.stringify(null)gives today for anulloutput; for anundefinedoutput today there is nocontentat all.To keep the order valid,
toOpenAIMessagescollects forwarded media intoolMediaand 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.tscover text only, output plus text and a resource, a standalone resource, adatapart next to acustomone, 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. OnetoOpenAIRequestBodytest checks thesupportsMediaflag. The existing output only cases are unchanged.What changes for a user
outputsend exactly what they sent before.Media (<type>) omittedline instead of an error.Media (<type>) omittedline on other models.dataparts, which were dropped silently, now reach the model as JSON text.Fixes #7211