Skip to content

fix(message): Responses tool image serialization (preserve file_editor images) - #1895

Merged
enyst merged 9 commits into
OpenHands:mainfrom
Wangmerlyn:fix/responses-image-content
Feb 6, 2026
Merged

enyst merged 9 commits into
OpenHands:mainfrom
Wangmerlyn:fix/responses-image-content

Conversation

@Wangmerlyn

Copy link
Copy Markdown
Contributor

Summary

Fixes the Responses API path dropping tool images. file_editor.view observations now deliver images to the model instead of stripping them. related to Issue #1894

What changed

  • Message.to_responses_dict (tool role):
    • When vision is enabled and ImageContent is present, emit message items with input_image entries (matching Responses API
      expectations).
    • Emit a minimal function_call_output alongside to keep call_id threading (no base64 stuffed into text).
  • Leaves TextContent -> function_call_output unchanged.

Rationale

Previously, tool-role serialization only sent TextContent; ImageContent was discarded, so models never saw images in Responses mode.
This aligns tool image handling with how user-role images are serialized (input_image).

Checklist

  • If the PR is changing/adding functionality, are there tests to reflect this?
  • If there is an example, have you run the example to make sure that it works?
  • If there are instructions on how to run the code, have you followed the instructions and made sure that it works?
  • If the feature is significant enough to require documentation, is there a PR open on the OpenHands/docs repository with the same branch name?
  • Is the github CI passing?

Co-authored-by: openhands <openhands@all-hands.dev>
@Wangmerlyn Wangmerlyn changed the title Fix Responses tool image serialization (preserve file_editor images) fix(message) Responses tool image serialization (preserve file_editor images) Feb 4, 2026
@Wangmerlyn Wangmerlyn changed the title fix(message) Responses tool image serialization (preserve file_editor images) fix(message): Responses tool image serialization (preserve file_editor images) Feb 4, 2026

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

The core fix for image serialization looks correct, but there are several important concerns around the logging infrastructure and design choices that should be addressed before merging.


🔴 Critical Issues

1. Performance Impact from Logging on Hot Path (Lines 470-472, 498-500, 550-552, 597-599)

Logging is being called on EVERY message serialization (hot path). This could significantly impact performance, especially since it:

  1. Calls json.dumps for previews on every message
  2. Performs string operations and truncation
  3. Potentially writes to disk on every call

Recommendations:

  • Make logging opt-in via a feature flag/environment variable (not just checking if LLM_PAYLOAD_LOG_PATH exists)
  • Only log when there are actual images
  • Use a proper logging level (DEBUG) that can be disabled in production
  • Move to async/background logging if disk writes are needed

2. Disk Space Risk (Lines 638-650)

This writes a NEW FILE for EVERY message serialized when LLM_PAYLOAD_LOG_PATH is set. This will:

  • Quickly fill up disk space in production
  • Create thousands of files during a single conversation
  • Impact performance with synchronous I/O on hot path

Solutions:

  1. Only log when explicitly enabled (separate env var from LLM_PAYLOAD_LOG_PATH)
  2. Use log rotation/size limits
  3. Sample (e.g., only log 1% of messages, or only messages with images)
  4. Use async I/O to avoid blocking
  5. Aggregate into a single file with entries, not one file per message

Also: The try/except silently continues on failure - at minimum log the error.

3. Tool→User Role Conversion Design (Lines 574-606)

This design converts tool role images to user role messages, which seems like a workaround. Questions:

  1. Why can't the Responses API handle images in tool responses directly?
  2. Could this confuse models that see "user" messages they never actually sent?
  3. Is the minimal function_call_output with just metadata acceptable to all consumers?
  4. What happens if there are multiple tool messages - will the inserted user messages break conversation flow?

Suggestions:

  • Add detailed docstring explaining why this conversion is necessary
  • Add validation that url is valid
  • Consider what happens with multiple images/multiple tool calls
  • Add integration tests showing this works end-to-end

🟠 Important Issues

4. Synthetic User Messages in Tool Context (Line 582)

The inserted message has role="user" but appears in the middle of tool responses. This could confuse:

  • Models that track conversation history
  • Debugging tools that follow the conversation flow
  • Other code that expects tool responses to be contiguous

Recommendation: Add a special marker or metadata to these synthetic user messages to distinguish them from real user input.

5. Missing Test Coverage

The PR description mentions tests should be added, but I don't see test changes in the diff. This functionality should have:

  • Unit tests for the ImageContent serialization path
  • Integration tests showing images flow through correctly
  • Tests for the logging functionality

🟡 Suggestions

6. Single Responsibility Principle Violation (Line 593)

The _log_responses_serialization method is 73 lines and mixes serialization with logging. Consider:

  1. Extract to a separate module (e.g., message_serialization_logger.py)
  2. Split into smaller functions:
    • _log_preview()
    • _write_payload_to_disk()
    • _should_log_payload() (for conditional logic)

This makes testing easier and keeps Message focused on serialization.

7. Import Documentation (Lines 2-4)

Add a comment explaining these imports are for the logging infrastructure:

import os
import time
import uuid
# Imports above used for _log_responses_serialization debugging

This helps readers understand why these seemingly unrelated imports exist in a message serialization module.


🟢 Nits

8. Emoji in Logs (Line 520)

The logging emoji (📦, 📝, 🗃️ ) might not render correctly in all logging environments (e.g., CloudWatch, Splunk). Consider:

  • Using plain text prefixes
  • Making emoji optional via config
  • Using ASCII art alternatives

Not critical, but could impact log readability in production.


Summary

The core image serialization fix (converting ImageContent to input_image messages) addresses the issue. However, the logging infrastructure needs significant refinement to avoid production issues. Please address the critical concerns before merging.

Co-authored-by: openhands <openhands@all-hands.dev>
{
"type": "message",
"role": "user",
"content": [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, should this really be a new user message, or directly part of items, with type=input_image?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OpenHands Understand the Responses API from openai docs, modify the reasoning example you can find in examples/ to accept an image (make yourself a screenshot and use that as the image, you can install yourself what you need) and run it. Use openai key and remote gh key to do this and verify the API calls work with an image.

Post your result directly on github, note that it will be rendered as markdown

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on it! enyst can track my progress at all-hands.dev

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist:

  • Updated the Responses reasoning example to accept an image (data URL).
  • Generated a screenshot image for the example input.
  • Ran the example with the OpenAI key and captured output.
  • Posted results as markdown on GitHub and pushed changes.

Changes since the last summary:

  • No additional code changes. Verified the branch is clean and fully pushed.

Final summary:

  • All requested updates are complete and already pushed; no further changes needed.

View full conversation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, should this really be a new user message, or directly part of items, with type=input_image?

Hi Enyst, thank you for the timely review. I chose this design to put the image data url under the role user simply because that I looked into the openai response api docs for function call output item and it says that the "output is free text and can contain any information or simply be empty."

I agree that it's much better to somehow put the image data url under the role tool like how we usually do in the chat completion api. But I can't find a good solution.

I tried something like

                          items.append(
                                  {
                                      "type": "function_call_output",
                                      "call_id": resp_call_id,
                                      "output": json.dumps(
                                          {
                                              "type": "image_url",
                                              "image_url": url,
                                          }
                                      ),
                                  }"

But it just feed the whole image data url as text into the agent. So I don't really have a solution for this and I chose to use the User role for a workaround.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry Enyst, it seems that I have a misunderstanding with the response api. I had looked into the wrong place for response api docus. Real openai response api docs for function call output item this is the correct documentation. I now have fixed the implementation to use function_call_output for image url.

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much! This looks great to me!

Do you think you could show a screenshot or some other way to see that it works when sent to the LLM from file editor or another tool? Just a real-world test, so we see.

Maybe we could also add a unit test or a modification of a relevant unit test to show that it works?

@Wangmerlyn

Wangmerlyn commented Feb 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you very much! This looks great to me!

Do you think you could show a screenshot or some other way to see that it works when sent to the LLM from file editor or another tool? Just a real-world test, so we see.

Maybe we could also add a unit test or a modification of a relevant unit test to show that it works?

Thank you, Engel. I can show a quick test result using one of my random photos to show that it works for the LLM to 'see' from the file_editor after the fix. (before/after fix comparison).

Later I'll see if I can add a relevant test case for this fix. Do you want the test case to be end to end running in the agent workflow or just a simple test to confirm that the image url part is in the responses api message dict?

@Wangmerlyn

Copy link
Copy Markdown
Contributor Author

This is the image I used to testing if the model is able to "see" the image through the ImageContent provided by file_editor. I used the model gpt-5 as the backend for the agent. I asked the agent to view the image by the tool file_editor and describe what it sees.
image used for testing

This is the final response before fixing, as you can see, the agent response is either full of hallucination or the agent says that it is not able to see the image, both are results of being told there is an image but in fact there is not.
image

image

This is after the bug fix, as you can see, the agent now correct describes what's in the picture.

after fix, correct answer

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🎉

@enyst
enyst merged commit 30e2209 into OpenHands:main Feb 6, 2026
18 checks passed
@enyst

enyst commented Feb 6, 2026

Copy link
Copy Markdown
Member

@OpenHands Understand this PR and read all top level comments on it.

Look at the integration tests, I think we have at least one that is about images. Look if we can modify it to add what this PR is exercising: an image that goes to the LLM via file_editor tool. We hadn’t seen the bug because we weren’t testing for tool use with the image in the tool, I suppose? If we can add an image there, open an issue with an implementation plan or code snippet how we’d do that. If not, open an issue to make a new test maybe, or what suggestions do you have.

@openhands-ai

openhands-ai Bot commented Feb 6, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

@openhands-ai

openhands-ai Bot commented Feb 6, 2026

Copy link
Copy Markdown

Final summary (new since last update):

Checklist:

  • Investigated PR context and relevant integration tests.
  • Identified existing image integration test (t08_image_file_viewing) as target for coverage.
  • Opened issue with implementation plan/snippet (no code changes per instruction).

No git diff to review; nothing to push.

View full conversation

StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…r images) (OpenHands#1895)

Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants