AWS Bedrock _parse_response: a valid empty content block list hits [0] and comes back as "Error parsing response"
Component
Python SDK
Description
Summary
AWSBedrockLLM._parse_response (mem0/llms/aws_bedrock.py) reads the first block of each provider's response with an unguarded [0]. When Bedrock returns a present-but-empty block list — a valid response; some stop conditions yield an empty content array, the case already acknowledged for the Converse path in #6368 — five provider branches raise IndexError (and the generic fallback has the same failure for a non-dict first element), which the function's broad except swallows into the sentinel string "Error parsing response". A valid empty response is therefore misclassified as a parse failure, and the sentinel flows downstream as if it were the model's answer.
On main @ 94c3fe9f (2026-09-28):
mem0/llms/aws_bedrock.py:399(anthropic):response_json.get("content", [{"text": ""}])[0].get("text", "")— the[{"text": ""}]default only applies when the key is missing;"content": []passes the guard and raises on[0].mem0/llms/aws_bedrock.py:405(amazon nova):response_json["content"][0]["text"]— no guard at all (and an implicitNonewhen neithercontentnorcompletionis present).mem0/llms/aws_bedrock.py:414(mistral),:416(cohere),:418(ai21): the same.get(key, [default])[0]pattern overoutputs/generations/completions.mem0/llms/aws_bedrock.py:424(generic fallback): the same[0].get(...)behind its emptiness guard — a non-dict first element still lands in the sentinel.
Of these, the live invoke_model path today reaches mistral / cohere / ai21 (anthropic text calls route through _generate_standard's Converse parsing, and the nova sub-branch is unreachable for a different reason — see #7360). The anthropic/nova sub-branches still carry the identical pattern and become live as those routings get fixed, so they are included.
Steps to Reproduce
No AWS credentials or network access are needed — _parse_response is offline. The ai21 body shape matches the existing cases in tests/llms/test_aws_bedrock.py; for the other providers the payloads use exactly the fields each branch itself reads.
import json
from mem0.llms.aws_bedrock import AWSBedrockLLM, extract_provider
class FakeBody:
def __init__(self, payload):
self._raw = json.dumps(payload).encode()
def read(self):
return self._raw
def make_llm(model):
llm = AWSBedrockLLM.__new__(AWSBedrockLLM)
llm.config = type("C", (), {"model": model})()
llm.provider = extract_provider(model, None)
return llm
CASES = [
("anthropic.claude-3-5-sonnet-20240620-v1:0", {"content": []}),
("anthropic.claude-3-5-sonnet-20240620-v1:0", {"content": [{"text": "hello"}]}),
("mistral.mistral-large-2402-v1:0", {"outputs": []}),
("mistral.mistral-large-2402-v1:0", {"outputs": [{"text": "hello"}]}),
("cohere.command-r-plus-v1:0", {"generations": []}),
("cohere.command-r-plus-v1:0", {"generations": [{"text": "hello"}]}),
("ai21.j2-mid-v1", {"completions": []}),
("ai21.j2-mid-v1", {"completions": [{"data": {"text": "hello"}}]}),
("amazon.nova-lite-v1:0", {"content": []}),
("amazon.nova-lite-v1:0", {"content": [{"text": "hello"}]}),
]
for model, payload in CASES:
out = make_llm(model)._parse_response({"body": FakeBody(payload)})
print(f"{model:<44} {json.dumps(payload):<38} {out!r}")Expected Behavior
A present-but-empty block list is a valid response; _parse_response should return "" for it, exactly as it already does when the key is missing (that is what each branch's [{"text": ""}]-style default is for).
Actual Behavior
Every empty-list case returns 'Error parsing response' (real output, main @ 94c3fe9f; the warning lines appear on stderr):
Could not parse response: list index out of range # x5, one per empty-list case
anthropic.claude-3-5-sonnet-20240620-v1:0 {"content": []} 'Error parsing response'
anthropic.claude-3-5-sonnet-20240620-v1:0 {"content": [{"text": "hello"}]} 'hello'
mistral.mistral-large-2402-v1:0 {"outputs": []} 'Error parsing response'
mistral.mistral-large-2402-v1:0 {"outputs": [{"text": "hello"}]} 'hello'
cohere.command-r-plus-v1:0 {"generations": []} 'Error parsing response'
cohere.command-r-plus-v1:0 {"generations": [{"text": "hello"}]} 'hello'
ai21.j2-mid-v1 {"completions": []} 'Error parsing response'
ai21.j2-mid-v1 {"completions": [{"data": {"text": "hello"}}]} 'hello'
amazon.nova-lite-v1:0 {"content": []} 'Error parsing response'
amazon.nova-lite-v1:0 {"content": [{"text": "hello"}]} 'hello'Environment
- mem0:
main@94c3fe9f(pip package version 2.2.1) - Python 3.12, macOS
How You Verified This
What I Ran
The script above against a clean checkout of main @ 94c3fe9f (no AWS credentials; parsing is offline). I additionally ran the full existing tests/llms/test_aws_bedrock.py suite on the same checkout (56 passed) to confirm the invoke_model body path has no empty-block-list case — the suite's existing empty-content test covers the Converse path instead.
What I Saw
The five empty-list cases each log Could not parse response: list index out of range and return the sentinel string, while the adjacent non-empty cases parse normally — the misclassification is purely the empty-list shape, not the body being malformed.
Why This Is a Bug
The code itself says so in three places in the same file:
- Each broken expression already contains the intended empty-outcome:
.get("content", [{"text": ""}])exists precisely to yield""when there are no blocks. The bug is only that the default fires on a missing key, not on a present-but-empty list, so the author's own intent doesn't hold for the empty-list shape. - The generic fallback branch right below (
mem0/llms/aws_bedrock.py:421-427) deliberately guards withisinstance(response_json[field], list) and response_json[field]before indexing — the same file already treats "empty list → skip, don't crash" as the rule. - The Anthropic Converse branch in
_generate_standard(mem0/llms/aws_bedrock.py:580-593, added for #6368) iterates the block list and returns""when no block carries text — the established pattern this file uses for exactly this outcome.
Interaction with #6548 / PR #6551: that issue (correctly) makes genuine parse failures raise instead of returning the sentinel. Without also fixing this misclassification, a valid empty response would then raise a spurious LLMError — worth resolving together so the two changes don't fight. Related: #7360 — once the Nova routing is fixed, the nova sub-branch's unguarded ["content"][0]["text"] at :405 becomes the live path with the same bug.
What I Ruled Out
- #6548 — same sentinel, different defect class: it wants parse failures to raise; the input here is not a parse failure.
- #7360 — Nova Converse/invoke routing mismatch; that failure happens at the
bodyread, before any provider branch is reached. - #6368 — reasoningContent-before-text on the Converse path, already merged; the
invoke_modelpath is untouched by it. - The generic
elsefallback branch's empty-list case is already guarded — it falls through to the next field, and the patch leaves that fall-through untouched. Only its non-dict-first-element case misclassifies (sentinel instead of""), and it is the sixth spot listed above. - Network/auth causes — parsing is fully offline and reproduced with the body mocked.
AI Assistance
Found and reproduced by an AI coding agent (Claude Code) working for this account. Every output above is pasted verbatim from real runs of the script shown; it needs no AWS credentials and finishes in ~2 seconds if anyone wants to re-run it.
A tested patch is ready (a shared first-block helper plus 10 regression and positive tests, local suite green). Per the PR gate I'll hold the pull request until this issue carries the accepted label — feel free to assign it to me.
Source: mem0ai/mem0