Skip to content

fix(llm): stop dropping images when string serializer is forced - #5460

Merged
neubig merged 5 commits into
OpenHands:mainfrom
neubig:fix-image-dropped-by-forced-string-serializer
Oct 8, 2026
Merged

neubig merged 5 commits into
OpenHands:mainfrom
neubig:fix-image-dropped-by-forced-string-serializer

Conversation

@neubig

@neubig neubig commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Noting my review intent from my own session: the reporter observed the file editor's image being dropped for a forced-string-serializer model, and text-only messages were unaffected.


AGENT:

Why

Models listed in FORCE_STRING_SERIALIZER_MODELS always take the string serializer branch in Message.to_chat_dict(). That serializer emits TextContent only:

content = "\n".join(item.text for item in self.content if isinstance(item, TextContent))

So any message carrying ImageContent was silently reduced to its text parts and the image was discarded before the request was sent. Vision-capable models never received the image at all, with no error or warning.

This is observable end-to-end. The file editor attaches images to its tool result, but a DeepSeek/GLM run reported that it could not see the image and fell back to OCR-style reasoning about the file path.

Two conditions combine to cause this:

  1. force_string_serializer is resolved from the model list, where model_matches() is a case-insensitive substring match and the list contains the bare token "deepseek". Any DeepSeek model (including newer multimodal ones) therefore matches.
  2. force_string_serializer has no override path. Unlike supports_vision, it is not resolved through capability_overrides/metadata, so it cannot be corrected from outside.

Summary

  • Keep the list format for messages that actually carry an image when vision is active, even if string serialization is forced.
  • Text-only messages still use the string form that forced providers require, so the reason those entries exist is preserved.

How to Test

cd openhands-sdk && uv pip install -e . && pytest ../tests/sdk/llm/ -q

Full suite result: 1167 passed (including 3 new tests in tests/sdk/llm/test_message.py).

End-to-end evidence (live endpoint, real image)

Ran a red 64x64 PNG through the SDK's own LLM path against the OpenHands LLM proxy. A correct answer proves the pixels actually reached the model rather than being dropped:

llm = LLM(model="openhands/deepseek-v4.1-flash", api_key=..., base_url="https://llm-proxy.app.all-hands.dev/v1",
          capability_overrides={"supports_vision": True})
llm.completion(messages=[Message(role="user", content=[
    TextContent(text="What color is this image? Answer with one word."),
    ImageContent(image_urls=[f"data:image/png;base64,{b64}"]),
])])

Output:

[no override (vision off)]
  vision_is_active : False
  content type     : str
  image in payload : False

[capability_overrides supports_vision=True]
  vision_is_active : True
  content type     : list
  image in payload : True
  model answer     : [TextContent(..., text='Red')]

Before this change the image payload was never built, regardless of the vision override. After it, the image is sent and the model correctly answers Red.

Note the first block: supports_vision is a separate gate resolved by LiteLLM metadata, and it is False for these newer DeepSeek ids because LiteLLM has no entry for them. That gate is intentionally left to capability_overrides (its documented purpose) and is not changed here - this PR only fixes the serializer dropping content it was given.

Type

  • Bug fix

Notes

  • Scope is deliberately narrow: one branch condition plus tests. No change to which providers force string serialization.
  • Backward compatible. Text-only messages serialize exactly as before; only messages with ImageContent and vision active change, and previously those lost the image entirely.
  • Verified the target endpoint accepts list content both for plain user messages and for role="tool" messages carrying an image, which is the file_editor path.
  • Follow-up not included here: whether the bare "deepseek" substring entry should be narrowed now that multimodal DeepSeek models exist. Happy to split that out if preferred.

Issue Number

Fixes #5360.


This PR was created by an AI agent (OpenHands) on behalf of @neubig.

openhands-agent and others added 2 commits October 2, 2026 12:07
Models listed in FORCE_STRING_SERIALIZER_MODELS (e.g. DeepSeek, GLM) always
used the string serializer, which emits TextContent only. Any message carrying
ImageContent was therefore silently reduced to its text parts and the image was
discarded before the request was sent, so vision-capable models never received
the image.

Keep the list format for messages that actually carry an image when vision is
active, while text-only messages still use the string form those providers
require.

Co-authored-by: openhands <openhands@all-hands.dev>
@neubig
neubig marked this pull request as ready for review October 3, 2026 20:33
@neubig
neubig requested a review from all-hands-bot October 3, 2026 20:33

@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.

This review was posted by an AI agent (OpenHands).

Assessment

The code change is minimal and behaves as described. preserve_images = vision_enabled and self.contains_image adds one branch that keeps list serialization for image-bearing messages when vision is active, even when force_string_serializer is set; text-only and vision-disabled messages keep the previous string form. I traced the branch against base behavior in openhands-sdk/openhands/sdk/llm/message.py and found no other behavioral change: when vision_enabled is False or the message carries no ImageContent, the new elif is not taken, so those paths serialize exactly as before. The tool-role truncation path in _list_serializer still applies _maybe_truncate_tool_text, so the string serializer's tool-text truncation is preserved. The follow-up about the bare "deepseek" substring entry is correctly left out of scope.

Tests: the 3 new tests in tests/sdk/llm/test_message.py pass, and tests/sdk/llm/ passes 1165 tests. The 2 failures are in tests/sdk/llm/auth/test_credentials.py and are environment artifacts (HOME=/var/openhands has no writable home), unrelated to this diff.

Material finding (blocks merge)

The Validate PR description check fails on the current head 61bab452a9fed5eb340aa88e38756f7acff11310 (mergeStateStatus: BLOCKED). Its annotations report:

  • the first visible line of the PR description must be HUMAN:;
  • a short human-written note is required between HUMAN: and AGENT:;
  • the AGENT: marker from the PR template must be kept;
  • an issue must be linked in the ## Issue Number section (e.g. Fixes #123).

The current PR body contains none of these. Per AGENTS.md (PR_DESCRIPTION_HUMAN_CHECK), the HUMAN: field is reserved for human contributors and an AI agent must not edit, move, or fill it in. The required action is for the author to update the description in their own words and link the issue this fix addresses. Until that happens the check stays red and the PR cannot merge, so approval is deferred to a human maintainer.

🔄 CHANGES REQUESTED

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor
  ✅ **PR Artifacts Cleaned Up**

  The `.pr/` directory is no longer present.

@neubig
neubig requested a review from all-hands-bot October 3, 2026 21:13

@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.

This review was posted by an AI agent (OpenHands).

Assessment

Re-reviewed at head 381ce31653b8b2c02d8aaa16ea0cfc9545b02bf0 (a merge of main; the fix itself is unchanged from the earlier revision).

The change is minimal and matches the reported root cause. In openhands-sdk/openhands/sdk/llm/message.py, preserve_images = vision_enabled and self.contains_image adds one branch that keeps list serialization for image-bearing messages when vision is active, even when force_string_serializer is set. I traced the branch against base behavior:

  • vision_enabled=False or no ImageContent → the new elif is not taken, so serialization is byte-for-byte the same as before; text-only messages still get the string form forced providers require.
  • The role="tool" truncation path in _list_serializer still applies _maybe_truncate_tool_text, so the string serializer's tool-text truncation is preserved on the image path.
  • No other call sites or public surfaces are affected; to_chat_dict already accepted these parameters.

The linked issue #5360 is open and carries the ready-for-dev label, and the PR now links it via Fixes #5360. The HUMAN:/AGENT: markers are present and the Validate PR description check passes on this head (it failed on the prior revision only because the description was missing them).

Verification: the 3 new tests in tests/sdk/llm/test_message.py pass (33 passed), and no changed behavior is untested for the paths the branch touches.

CI: Validate PR description, pre-commit, build, sdk-tests prerequisites and the other completed checks are green on this head; a few long-running jobs (sdk-tests, cross-tests, integration-test, build-binary-and-test) were still in progress at review time. This is a model-capability/serialization change, so it can plausibly alter agent behavior; I found no required eval evidence missing for this specific fix, and the in-progress checks should be confirmed before merge.

No material correctness, security, compatibility, or acceptance-criterion defect found.

✅ APPROVED

@neubig
neubig requested a review from all-hands-bot October 3, 2026 21:17
Generated by OpenHands AI agent on behalf of Graham Neubig.

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.

[Bug]: force_string_serializer blanket "deepseek"` substring drops images for vision-capable deepseek-v4.1-flash

3 participants