Skip to content

fix(openai): deliver media from tool results in a user message - #3889

Open
wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/issue-3882-tool-media-user-message
Open

wahllllll wants to merge 1 commit into
agentscope-ai:mainfrom
wahllllll:fix/issue-3882-tool-media-user-message

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

Problem

Chat Completions accepts media parts only in user messages. For a tool result that carries an image, OpenAIMessageConverter.convertToolMessage built a multimodal content-part array directly on the tool message:

{"role": "tool", "tool_call_id": "call_123",
 "content": [{"type": "text", "text": "Screenshot taken"},
             {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}}]}

That body is not valid for the Chat Completions schema. OpenAIMessage.content is typed as Object, so it serialized without a warning, and OpenAI-compatible providers either reject the request or silently drop the media — a tool that returns a screenshot has its output lost.

AbstractBaseFormatter.convertToolResultToString already produces a text-only representation (URL, or a temp path for base64 media), but the media branch made it unreachable.

Change

  • convertToolMessage always builds a text-only tool message, using that existing representation.
  • New convertToolMediaToUserMessage(msg) returns the user message that carries the media, naming the tool call, or null when the result has no media.
  • OpenAIChatFormatter emits both: doFormat now goes through a new convertMessages(msg, hasMedia) hook, so provider formatters that extend OpenAIChatFormatter (DeepSeek, GLM, …) inherit the split instead of each having to repeat it. Cache-control directives stay aligned with the emitted list.

Text-only tool results are unchanged: exactly one message, same content as before.

Compatibility

This changes the request body shape only. Journals, Msg objects and the tool-result conversion at the AgentScope level are untouched; nothing about how results are stored or read changes. The invalid shape was never something a compliant provider could consume.

Tests

  • OpenAIToolMediaPromotionTest.toolImageIsPromotedToAFollowingUserMessage asserts two messages: a text-only tool message with the call id, then a multimodal user message. Before the change it fails with expected: <2> but was: <1>.
  • textOnlyToolResultStaysASingleMessage pins that non-media results are not split.
  • OpenAIMessageConverterTest.testToolMessageWithMultimodalResult previously asserted the invalid shape (assertTrue(result.isMultimodal())); it now asserts the tool message stays text-only.

Verification

  • mvn -B -pl agentscope-extensions/.../agentscope-extensions-model-openai test: 581 tests, 0 failures, 0 errors.
  • spotless:check: BUILD SUCCESS.

Addresses #3882.

Chat Completions rejects media parts inside a role=tool message, so a tool that
returned an image, audio or video block produced a request the provider refused:

  Invalid value for 'content': expected a string, got array

A tool result that carries media is now split in two: the tool message keeps the
text (media described by reference, the way convertToolResultToString already
does), and a user message with the media parts follows it, labelled with the call
it belongs to.

Documents keep the behaviour added for them separately: they are described by
reference inside the tool message text and are not repeated in the media message.
@wahllllll
wahllllll force-pushed the fix/issue-3882-tool-media-user-message branch from ae0b55b to f4919b3 Compare November 7, 2026 13:42
@wahllllll

Copy link
Copy Markdown
Contributor Author

Rebased onto main — the branch was 43 commits behind and had gone dirty.

One real conflict, in convertToolMessage, because #3979 added extractTextWithDocumentReferences. The resolution keeps both: the no-result fallback now uses the document-aware extraction, while tool messages stay text-only, which is what this PR is about.

Worth flagging for review: testMixedToolResultKeepsDocumentReference (added by #3979) asserted that a mixed tool result comes back as a list of parts with the image inside role=tool — the exact shape OpenAI rejects, so the test had encoded the bug as its expectation. I rewrote it to assert what survives both changes:

  • the tool message is a string and still carries the tool text plus the document reference;
  • the image arrives in a follow-up user message;
  • the document is not repeated there, since a document is a reference rather than a part.

If the intent of #3979 was for documents to be parts rather than references, that is a different design and I would rather hear it than quietly keep the reference.

While resolving it I also dropped documents from the media message for the same reason — otherwise the model would see the same reference twice.

mvn -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai test: 593 tests, 0 failures, 11 skipped (the E2E suite that needs a real API key).

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

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants