Skip to content

Handle Anthropic usage=None on content-filtered responses#8647

Merged
Soulter merged 2 commits into
masterfrom
copilot/fix-attribute-error-usage-none
Jun 7, 2026
Merged

Handle Anthropic usage=None on content-filtered responses#8647
Soulter merged 2 commits into
masterfrom
copilot/fix-attribute-error-usage-none

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 7, 2026

Anthropic-compatible providers can return stop_reason=content_filter with completion.usage=None. The non-stream query path assumed usage was always present and could crash with AttributeError when extracting token counters.

  • Usage extraction hardening

    • Updated ProviderAnthropic._extract_usage to accept Usage | None.
    • Returns zeroed TokenUsage when usage is missing.
    • Normalizes nullable output tokens with or 0 to keep token accounting safe.
  • Regression coverage for filtered responses

    • Added a focused test in tests/test_anthropic_kimi_code_provider.py for content-filtered completions with usage=None.
    • Verifies response text is preserved and usage is populated as zero values instead of raising.
def _extract_usage(self, usage: Usage | None) -> TokenUsage:
    if usage is None:
        return TokenUsage()
    return TokenUsage(
        input_other=usage.input_tokens or 0,
        input_cached=usage.cache_read_input_tokens or 0,
        output=usage.output_tokens or 0,
    )

Summary by Sourcery

Handle Anthropic-compatible content-filtered responses that may omit usage data and ensure token accounting remains safe.

Bug Fixes:

  • Prevent crashes when Anthropic-compatible providers return content-filtered responses with null usage by defaulting to zeroed token usage.

Tests:

  • Add an async test verifying that content-filtered Anthropic responses with missing usage preserve completion text and populate zero-valued usage counters.

Copilot AI changed the title [WIP] Fix AttributeError when content_filter causes usage to be None Handle Anthropic usage=None on content-filtered responses Jun 7, 2026
Copilot AI requested a review from Soulter June 7, 2026 06:56
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 7, 2026
@Soulter Soulter marked this pull request as ready for review June 7, 2026 07:29
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 7, 2026
@Soulter Soulter merged commit 1a04998 into master Jun 7, 2026
1 check passed
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In the new test, monkeypatch.setattr(anthropic_source, "Message", _FakeMessage) appears unused since provider.client.messages.create is directly patched to return _FakeMessage; consider removing this monkeypatch to keep the test focused and avoid confusion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the new test, `monkeypatch.setattr(anthropic_source, "Message", _FakeMessage)` appears unused since `provider.client.messages.create` is directly patched to return `_FakeMessage`; consider removing this monkeypatch to keep the test focused and avoid confusion.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] content_filter 响应导致 usage 为 None,触发 AttributeError 崩溃

2 participants