Repository navigation
Fix truncation at LLM message layer - #1838
Conversation
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
all-hands-bot
left a comment
There was a problem hiding this comment.
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! ✅
|
@OpenHands Understand this PR, and address from the last comment:
Commit, precommit, push. |
|
I'm on it! enyst can track my progress at all-hands.dev |
Co-authored-by: openhands <openhands@all-hands.dev>
Final summary (new since last summary)What changed
Verification / hygiene
Checklist vs request
Commit pushed: |
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like 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
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
I don't think this fix will work for list_serializer, right?
There was a problem hiding this comment.
I'm on it! xingyaoww can track my progress at all-hands.dev
There was a problem hiding this comment.
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:
_string_serializer()- truncation at lines 336-337_list_serializer()- truncation at lines 362-366
|
[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
left a comment
There was a problem hiding this comment.
LGTM - Thank you!
I ran into similar issue earlier and i think this PR is a good idea :)
Co-authored-by: openhands <openhands@all-hands.dev>
HUMAN:
We do a lot of truncations in multiple places (9 of them 😅 ):
I've repeatedly seen this one in the logs for
behaviorintegration tests, so I investigated it in #1813 , and I think currently it's not ideal: it's truncatingSkills. The LLM truncation is the "last line of defense" before we send outputs to the LLM. Its reason is to avoid sending hugebrowserorbashpayloads, 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:
role=toolobsresponses()path.Re: Agent Review
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
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22golang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:73fb2b8-pythonRun
All tags pushed for this build
About Multi-Architecture Support
73fb2b8-python) is a multi-arch manifest supporting both amd64 and arm6473fb2b8-python-amd64) are also available if needed