microsoft / microsoft/StableQAT
`anthropic_chat()` crashes with IndexError when API response content is empty or filtered
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
anthropic_chat() crashes with IndexError when API response content is empty or filtered
Repository: microsoft/StableQAT
Description
The anthropic_chat() function in lm_eval/models/anthropic_llms.py accesses response.content[0].text without verifying that response.content is non-empty or that the first content block is a TextBlock:
def messages():
response = client.messages.create(
model=model,
max_tokens=max_tokens,
temperature=temperature,
messages=[{"role": "user", "content": f"{prompt}"}],
**kwargs,
)
return response.content[0].text # line 139
This crashes with:
IndexErrorifresponse.contentis an empty list (e.g., the model's response was filtered by Anthropic's safety systems, or the API returned astop_reasonof"end_turn"with no content)AttributeErrorifcontent[0]is not aTextBlock(e.g., it's aToolUseBlockin newer API versions)
Content filtering is increasingly common with safety-tuned Anthropic models, making this a realistic failure mode during evaluation.
Suggested fix
def messages():
response = client.messages.create(
model=model,
max_tokens=max_tokens,
temperature=temperature,
messages=[{"role": "user", "content": f"{prompt}"}],
**kwargs,
)
if not response.content:
return ""
block = response.content[0]
return block.text if hasattr(block, "text") else ""
How this was found
This was identified via static analysis using a3-python, which flagged the unguarded index access as a DSE-confirmed reachable NULL_PTR (potential IndexError/AttributeError).
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lm_eval/models/anthropic_llms.py at the anthropic_chat() implementation around line 139, and inspect how the Anthropic response content is handled. Exercise the empty-content and non-text-block cases, then verify that the function returns safely without raising IndexError or AttributeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100