Skip to content

feat: add ProviderTokenCountError for native token counting failures#2211

Merged
opieter-aws merged 2 commits intostrands-agents:mainfrom
opieter-aws:feat/provider-token-count-error
Apr 28, 2026
Merged

feat: add ProviderTokenCountError for native token counting failures#2211
opieter-aws merged 2 commits intostrands-agents:mainfrom
opieter-aws:feat/provider-token-count-error

Conversation

@opieter-aws
Copy link
Copy Markdown
Contributor

Description

Add ProviderTokenCountError to the Python SDK, matching the typed error introduced in the TypeScript SDK (sdk-typescript#886). This error is thrown when a model provider's native token counting API returns an unexpected null result, and is used as internal control flow within provider count_tokens() overrides.

TypeScript port of

Related Issues

#2179

Documentation PR

Will update as part of proactive compression doc updated

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare
  • Existing TestCountTokens tests for Bedrock and Gemini (including test_fallback_on_none_total_tokens, test_fallback_on_api_error, test_fallback_on_generic_exception) continue to pass — 188
    tests, 0 failures

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread src/strands/types/exceptions.py
@github-actions
Copy link
Copy Markdown

Assessment: Approve

Clean, well-scoped PR that introduces a typed exception for null token count responses, replacing a generic ValueError in Gemini and adding null-safety to Bedrock. The change is purely internal (not exported in the public API), follows existing exception patterns, and has good test coverage.

Review Details
  • Consistency: The new exception is only adopted in Bedrock and Gemini — the other providers with count_tokens overrides (Anthropic, OpenAI Responses, llama.cpp) don't use it. Worth considering whether those should be audited for null-safety as a follow-up.
  • Design: Using an exception for internal control flow (null API response → fallback) is a valid pattern here since it keeps the try/except fallback path uniform across all failure modes, and aligns with the TypeScript SDK.

Solid incremental improvement to the token counting robustness. 👍

@opieter-aws opieter-aws marked this pull request as ready for review April 28, 2026 12:16
Comment thread src/strands/types/exceptions.py
@github-actions
Copy link
Copy Markdown

Assessment: Approve

Clean, well-scoped PR that introduces a typed internal exception for null token count responses — replacing a generic ValueError in Gemini and adding null-safety to Bedrock's previously unsafe direct dict access. No public API surface change.

Review Details
  • Terminology: The docstring uses "Thrown" instead of "Raised", breaking the consistent pattern used by every other exception in exceptions.py.
  • Cross-provider consistency: Only Bedrock and Gemini adopt ProviderTokenCountError. Anthropic and OpenAI Responses access response.input_tokens without null guards, though their SDK types declare int (not Optional[int]), making this lower risk. Acceptable scope for this PR.

@opieter-aws opieter-aws merged commit 009374f into strands-agents:main Apr 28, 2026
54 of 65 checks passed
@opieter-aws opieter-aws deleted the feat/provider-token-count-error branch April 28, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants