fix(backends/litellm): tolerate a bare string in tool_result content list (#3795)

## Description

`_convert_messages_for_litellm` flattens a list-shaped `tool_result`
content like this:

```python
if isinstance(tr_content, list):
    tr_content = "\n".join(
        b.get("text", "") for b in tr_content if b.get("type") == "text"
    )
```

This assumes every element of the list is a dict. But a client may put a
**bare string** in the list (`content: ["result text"]` — a valid
Anthropic tool_result shape). `b.get(...)` on a `str` raises
`AttributeError: 'str' object has no attribute 'get'`, which 500s the
entire request via `--backend litellm` / bedrock.

Confirmed:

```python
_convert_messages_for_litellm([{"role":"user","content":[
    {"type":"tool_result","tool_use_id":"tu_1","content":["a bare string result"]}]}])
# -> AttributeError: 'str' object has no attribute 'get'
```

Fix: accept bare strings (contribute their text), keep
`{"type":"text",...}` dict blocks, and skip other block types — the same
guard the sibling any-llm converter uses. Plain-string content and
text-block lists are unchanged.

(The neighboring missing-`tool_use_id` / missing-`id` `KeyError`s are
intentionally left as-is: defaulting `tool_call_id` to `""` would
produce an unroutable tool message and mask the error with a confusing
downstream failure, so a hard failure there is preferable to a
silent-wrong request. This PR is scoped to the unambiguous bare-string
crash.)

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/backends/litellm.py`: the `tool_result` list-content flatten
now handles bare-string elements and skips non-text blocks, using a
dedicated `text_pieces` local (avoids colliding with the function's
existing `list[dict]` `parts`).
- `tests/test_backend_bugs.py`: added
`test_tool_result_list_content_with_bare_string_block` — a list mixing a
bare string, a text block, and an image block flattens to the two text
pieces without raising.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality

### Test Output

```text
tests/test_backend_bugs.py -k ConvertMessagesToolBlocks  ->  8 passed
uvx ruff@0.16.8 check headroom/backends/litellm.py tests/test_backend_bugs.py  ->  All checks passed!
uvx mypy@1.20.2 headroom/backends/litellm.py  ->  clean (no error in this file)
```

## Real Behavior Proof

- Environment: Windows 11, Python 3.12.11, project venv, pytest 9.1.1,
ruff 0.16.8 and mypy 1.20.2 via uvx.
- Exact command / steps: called `_convert_messages_for_litellm` with a
`tool_result` whose content list contains a bare string — before the fix
it raised `AttributeError`; after, the tool message content is the
joined text. Verified plain-string content, text-block lists, and mixed
bare-string+text+image lists all produce the right joined text with no
crash. Red/green: the new test fails (AttributeError) with the fix
reverted; all 8 tool-block tests pass with it.
- Observed result: a tool continuation whose result content is a list
containing a bare string no longer 500s the request.
- Not tested: a live litellm/bedrock round-trip (the message conversion
is covered by unit tests).

## Runtime Rollout Safety

- Rollout-managed feature(s): none.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: no for conformant inputs (plain
string, text-block lists convert identically); a previously-crashing
bare-string list now converts instead of 500ing.
- Kill switch / disable path: N/A.
- Unsafe override required: no.
- Qualification impact: none.
- Rollback path: revert this commit; a bare-string tool_result content
element again raises AttributeError.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation (N/A:
internal message conversion)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

Mirrors the `isinstance(b, dict)` guard used by the any-llm backend's
tool_result handling, so both backends tolerate the same client input
shape.

Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Abhay Singh
2026-09-29 16:38:39 -05:00
committed by GitHub
co-authored by JD Davis
parent f88c10df6a
commit 8ff46dc959
2 changed files with 39 additions and 3 deletions
+12 -3
View File
@@ -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"],
+27
View File
@@ -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()