Skip to content

Fix truncation at LLM message layer - #1838

Merged
xingyaoww merged 9 commits into
mainfrom
openhands/remove-llm-textcontent-truncation
Feb 4, 2026
Merged

xingyaoww merged 9 commits into
mainfrom
openhands/remove-llm-textcontent-truncation

Conversation

@enyst

@enyst enyst commented Jan 27, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
We do a lot of truncations in multiple places (9 of them 😅 ):

I've repeatedly seen this one in the logs for behavior integration tests, so I investigated it in #1813 , and I think currently it's not ideal: it's truncating Skills. The LLM truncation is the "last line of defense" before we send outputs to the LLM. Its reason is to avoid sending huge browser or bash payloads, not Skills.

Problem: it's truncating everything text, which includes the system prompt (!), skills (this was why it was in behavior logs), and user messages.

Proposal:

  • truncate only the text in role=tool obs
  • (additionally) add it to the responses() path.

Re: Agent Review

🟠 Important: Inconsistent Truncation Between Serialization Paths

I don't really agree it's important. 🤔
It seems to me that we only need a best-effort to avoid 10x the common token size. This truncation should, ideally, be unnecessary, it's there "just in case" of emergency - super long observation.
(that, and string serializer is becoming less used, as more LLMs get multimodal or providers update their APIs)


Summary

Stop truncating non-tool content at the LLM-message serialization layer. Instead, apply an emergency 50k truncation only for tool outputs (role="tool"), covering both list- and string-serialization paths.

This preserves full system prompts (including skills), user messages, and assistant messages while still guarding against runaway tool observations.

Fixes #1813.

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?

Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22 Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:73fb2b8-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-73fb2b8-python \
  ghcr.io/openhands/agent-server:73fb2b8-python

All tags pushed for this build

ghcr.io/openhands/agent-server:73fb2b8-golang-amd64
ghcr.io/openhands/agent-server:73fb2b8-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:73fb2b8-golang-arm64
ghcr.io/openhands/agent-server:73fb2b8-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:73fb2b8-java-amd64
ghcr.io/openhands/agent-server:73fb2b8-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:73fb2b8-java-arm64
ghcr.io/openhands/agent-server:73fb2b8-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:73fb2b8-python-amd64
ghcr.io/openhands/agent-server:73fb2b8-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-amd64
ghcr.io/openhands/agent-server:73fb2b8-python-arm64
ghcr.io/openhands/agent-server:73fb2b8-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-arm64
ghcr.io/openhands/agent-server:73fb2b8-golang
ghcr.io/openhands/agent-server:73fb2b8-java
ghcr.io/openhands/agent-server:73fb2b8-python

About Multi-Architecture Support

  • Each variant tag (e.g., 73fb2b8-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 73fb2b8-python-amd64) are also available if needed

Co-authored-by: openhands <openhands@all-hands.dev>
@openhands-ai openhands-ai Bot mentioned this pull request Jan 27, 2026
@github-actions

github-actions Bot commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/llm
   message.py295996%246, 402, 415–416, 424, 466, 563, 702–703
TOTAL16432479170% 

enyst and others added 3 commits January 27, 2026 09:47
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@enyst enyst changed the title Remove TextContent truncation at LLM message layer Fix TextContent truncation at LLM message layer Jan 27, 2026
@enyst enyst changed the title Fix TextContent truncation at LLM message layer Fix truncation at LLM message layer Jan 27, 2026
enyst and others added 2 commits January 27, 2026 11:27
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@enyst
enyst marked this pull request as ready for review January 27, 2026 12:24

@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

Overall the approach looks good - moving truncation from TextContent to Message level and applying it only to tool outputs preserves system prompts and user messages while guarding against runaway tool observations. However, I found some issues that should be addressed:

🟠 Important: Inconsistent Truncation Between Serialization Paths

Location: openhands-sdk/openhands/sdk/llm/message.py lines 333-337 vs 362-366

There's a behavioral inconsistency between the string and list serializers:

  • String serializer (lines 333-337): Joins ALL TextContent items first, then truncates the combined result to 50k
  • List serializer (lines 362-366): Truncates each TextContent item individually

Example scenario:

  • Tool message with two TextContent items: 60k chars each
  • String path: Joins to 120k → truncates to 50k ✓
  • List path: Truncates each to 50k → total 100k ✗

Suggested fix: For consistency, the list serializer should also consider the total size across all text items, or document why per-item truncation is intentional.

🟡 Suggestion: Performance Optimization

Location: openhands-sdk/openhands/sdk/llm/message.py line 362

The self.role == "tool" check happens inside the loop for every item, even though the role is constant for the message. This could be moved outside:

# Truncate tool message text content if needed
is_tool_role = self.role == "tool"

for item in self.content:
    item_dicts = item.to_llm_dict()
    
    if is_tool_role and item_dicts:
        for d in item_dicts:
            ...

🟢 Nit: Unnecessary Conditional

Location: openhands-sdk/openhands/sdk/llm/message.py line 554

The if output_text: check before truncation is unnecessary since _maybe_truncate_tool_text already handles empty/short strings by checking length:

output_text = c.text
if output_text:  # <-- This check is redundant
    output_text = self._maybe_truncate_tool_text(output_text)

Could simplify to:

output_text = self._maybe_truncate_tool_text(c.text)

This also makes the code more consistent with the other truncation call sites (lines 337, 366).


Test Coverage

The test coverage looks comprehensive with tests for both serialization paths. Good work on that! ✅

@enyst

enyst commented Jan 27, 2026

Copy link
Copy Markdown
Member Author

@OpenHands Understand this PR, and address from the last comment:

  • the "Suggestion: Performance Optimization"
  • the "Nit: Unnecessary Conditional"

Commit, precommit, push.

@openhands-ai

openhands-ai Bot commented Jan 27, 2026

Copy link
Copy Markdown

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

Co-authored-by: openhands <openhands@all-hands.dev>
@openhands-ai

openhands-ai Bot commented Jan 27, 2026

Copy link
Copy Markdown

Final summary (new since last summary)

What changed

  • Removed an unnecessary conditional in Message.to_responses_dict() for tool messages by calling _maybe_truncate_tool_text(c.text) directly (instead of checking if output_text: first).
  • Hardened _maybe_truncate_tool_text to early-return on empty/falsey input (if not text or len(text) <= limit), so callers don’t need to guard for empty strings.

Verification / hygiene

  • git status: clean (no uncommitted changes).
  • pre-commit run -a: passed.
  • Changes are concise: only openhands-sdk/openhands/sdk/llm/message.py changed for these review items.

Checklist vs request

  • Addressed “Suggestion: Performance Optimization”
  • Addressed “Nit: Unnecessary Conditional”
  • Committed
  • Ran pre-commit
  • Pushed to openhands/remove-llm-textcontent-truncation

Commit pushed: 70f2895a (“Tighten tool output truncation helper”).

View full conversation

@openhands-ai

openhands-ai Bot commented Jan 27, 2026

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Run tests

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #1838 at branch `openhands/remove-llm-textcontent-truncation`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@xingyaoww xingyaoww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree on the general idea -- it is also pretty bad to truncate "user" message since it is not recoverable

if not force_string_serializer and (
cache_enabled or vision_enabled or function_calling_enabled
):
message_dict = self._list_serializer(vision_enabled=vision_enabled)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this fix will work for list_serializer, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@OpenHands can you investigate?

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! xingyaoww 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.

Answer to the Question

The reviewer asked: "I don't think this fix will work for list_serializer, right?"

Answer: Yes, the fix DOES work for list_serializer.

Looking at the code in message.py, the truncation is applied inside _list_serializer() at lines 362-366:

if self.role == "tool" and item_dicts:
    for d in item_dicts:
        text_val = d.get("text")
        if d.get("type") == "text" and isinstance(text_val, str):
            d["text"] = self._maybe_truncate_tool_text(text_val)

This code iterates through all content items in the list serializer output and applies truncation to any text content when the message role is "tool".

The test test_tool_message_truncates_text_over_limit specifically verifies this behavior by using cache_enabled=True (which triggers the list_serializer path) and confirms that truncation is applied correctly. I ran this test and it passes.

So the PR correctly handles truncation in both serialization paths:

  1. _string_serializer() - truncation at lines 336-337
  2. _list_serializer() - truncation at lines 362-366

View full conversation

@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @enyst, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

@xingyaoww xingyaoww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - Thank you!

I ran into similar issue earlier and i think this PR is a good idea :)

@xingyaoww
xingyaoww merged commit 8ac82ba into main Feb 4, 2026
33 checks passed
@xingyaoww
xingyaoww deleted the openhands/remove-llm-textcontent-truncation branch February 4, 2026 14:46
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
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.

Truncating at 50k

3 participants