Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(backends/litellm): tolerate a bare string in tool_result content list (#3795)
Abhay Singh8ff46dc9595a
diff --git a/headroom/backends/litellm.py b/headroom/backends/litellm.pyindex 805cf6df8..ac3aad7ac 100644--- a/headroom/backends/litellm.py+++ b/headroom/backends/litellm.py@@ -997,9 +997,18 @@ class LiteLLMBackend(Backend):for tr in tool_result_blocks:tr_content = tr.get("content", "")if isinstance(tr_content, list):- tr_content = "\n".join(- b.get("text", "") for b in tr_content if b.get("type") == "text"- )+ # A tool_result content list is usually+ # ``{"type":"text",...}`` blocks, but a client may put+ # a bare string in the list. ``b.get`` on a str raised+ # AttributeError and 500'd the whole request; accept+ # bare strings and skip non-text/other blocks.+ text_pieces: list[str] = []+ for b in tr_content:+ if isinstance(b, str):+ text_pieces.append(b)+ elif isinstance(b, dict) and b.get("type") == "text":+ text_pieces.append(b.get("text", ""))+ tr_content = "\n".join(text_pieces)tool_msg: dict[str, Any] = {"role": "tool","tool_call_id": tr["tool_use_id"],diff --git a/tests/test_backend_bugs.py b/tests/test_backend_bugs.pyindex e022e8a7b..26b0b059e 100644--- a/tests/test_backend_bugs.py+++ b/tests/test_backend_bugs.py@@ -274,6 +274,33 @@ class TestConvertMessagesToolBlocks:assert converted[0]["role"] == "tool"assert converted[0]["content"] == "Line 1\nLine 2"+ def test_tool_result_list_content_with_bare_string_block(self):+ """A tool_result content list may contain a bare string, not just+ ``{"type":"text",...}`` blocks. ``b.get`` on a str raised AttributeError+ and 500'd the whole request; bare strings must be accepted and other+ block types skipped."""+ backend = self._make_backend()+ messages = [+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "toolu_03",+ "content": [+ "bare string result",+ {"type": "text", "text": "typed block"},+ {"type": "image", "source": {"type": "base64", "data": "x"}},+ ],+ },+ ],+ },+ ]+ converted = backend._convert_messages_for_litellm(messages)+ assert converted[0]["role"] == "tool"+ # bare string + text block joined; the image block is skipped.+ assert converted[0]["content"] == "bare string result\ntyped block"+def test_assistant_tool_use_with_text(self):"""Assistant message with both text and tool_use blocks."""backend = self._make_backend()