Repository navigation
fix(message): Responses tool image serialization (preserve file_editor images) - #1895
Conversation
Co-authored-by: openhands <openhands@all-hands.dev>
all-hands-bot
left a comment
There was a problem hiding this comment.
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:
- Calls
json.dumpsfor previews on every message - Performs string operations and truncation
- 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:
- Only log when explicitly enabled (separate env var from LLM_PAYLOAD_LOG_PATH)
- Use log rotation/size limits
- Sample (e.g., only log 1% of messages, or only messages with images)
- Use async I/O to avoid blocking
- 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:
- Why can't the Responses API handle images in tool responses directly?
- Could this confuse models that see "user" messages they never actually sent?
- Is the minimal
function_call_outputwith just metadata acceptable to all consumers? - 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
urlis 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:
- Extract to a separate module (e.g.,
message_serialization_logger.py) - 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 debuggingThis 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": [ |
There was a problem hiding this comment.
I wonder, should this really be a new user message, or directly part of items, with type=input_image?
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
I'm on it! enyst can track my progress at all-hands.dev
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I wonder, should this really be a new user message, or directly part of
items, withtype=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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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? |
Co-authored-by: openhands <openhands@all-hands.dev>
|
@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 |
|
I'm on it! enyst can track my progress at all-hands.dev |
|
Final summary (new since last update):
Checklist:
No git diff to review; nothing to push. |
…r images) (OpenHands#1895) Co-authored-by: openhands <openhands@all-hands.dev>




Summary
Fixes the Responses API path dropping tool images.
file_editor.viewobservations now deliver images to the model instead of stripping them. related to Issue #1894What changed
Message.to_responses_dict(tool role):ImageContentis present, emitmessageitems withinput_imageentries (matching Responses APIexpectations).
function_call_outputalongside to keep call_id threading (no base64 stuffed into text).TextContent -> function_call_outputunchanged.Rationale
Previously, tool-role serialization only sent
TextContent;ImageContentwas discarded, so models never saw images in Responses mode.This aligns tool image handling with how user-role images are serialized (input_image).
Checklist