microsoft / microsoft/StableQAT

`anthropic_chat()` crashes with IndexError when API response content is empty or filtered

Open
#9 0 comments 0 reactions 0 assignees View on GitHub

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:

  • IndexError if response.content is an empty list (e.g., the model's response was filtered by Anthropic's safety systems, or the API returned a stop_reason of "end_turn" with no content)
  • AttributeError if content[0] is not a TextBlock (e.g., it's a ToolUseBlock in 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.