Repository navigation
Conversation
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.
ae0b55b to
f4919b3
Compare
|
Rebased onto One real conflict, in Worth flagging for review:
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.
|
Problem
Chat Completions accepts media parts only in user messages. For a tool result that carries an image,
OpenAIMessageConverter.convertToolMessagebuilt a multimodal content-part array directly on thetoolmessage:{"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.contentis typed asObject, 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.convertToolResultToStringalready produces a text-only representation (URL, or a temp path for base64 media), but the media branch made it unreachable.Change
convertToolMessagealways builds a text-onlytoolmessage, using that existing representation.convertToolMediaToUserMessage(msg)returns the user message that carries the media, naming the tool call, ornullwhen the result has no media.OpenAIChatFormatteremits both:doFormatnow goes through a newconvertMessages(msg, hasMedia)hook, so provider formatters that extendOpenAIChatFormatter(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,
Msgobjects 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.toolImageIsPromotedToAFollowingUserMessageasserts two messages: a text-onlytoolmessage with the call id, then a multimodalusermessage. Before the change it fails withexpected: <2> but was: <1>.textOnlyToolResultStaysASingleMessagepins that non-media results are not split.OpenAIMessageConverterTest.testToolMessageWithMultimodalResultpreviously 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.