Skip to content

fix(async): preserve typed errors in research stream mode#168

Open
shuofengzhang wants to merge 1 commit intotavily-ai:masterfrom
shuofengzhang:fix/async-research-stream-error-propagation
Open

fix(async): preserve typed errors in research stream mode#168
shuofengzhang wants to merge 1 commit intotavily-ai:masterfrom
shuofengzhang:fix/async-research-stream-error-propagation

Conversation

@shuofengzhang
Copy link
Copy Markdown

@shuofengzhang shuofengzhang commented Apr 27, 2026

What changed

  • Fixed AsyncTavilyClient research streaming error handling so Tavily-specific exceptions are no longer wrapped into a generic Exception.
  • Added explicit pass-through for:
    • UsageLimitExceededError
    • ForbiddenError
    • InvalidAPIKeyError
    • BadRequestError
    • TimeoutError
  • Added a regression test that verifies async streaming research preserves UsageLimitExceededError on HTTP 429.

Insight / Why this matters

In async stream mode, _research(..., stream=True) raised domain-specific exceptions for non-200 responses, but then immediately caught them with a broad except Exception and re-raised a generic error.

That made stream-mode error handling inconsistent with non-stream mode and with the sync client path. It is easy to miss because happy-path stream tests pass, while typed error handling only appears in failure scenarios.

This change preserves typed exceptions, so callers can implement correct retry/backoff and user messaging logic based on error class instead of brittle string parsing.

Practical gain / Why this matters

  • Improves production reliability for streaming consumers.
  • Enables precise client behavior (e.g., handle quota limits differently from auth failures).
  • Reduces integration bugs caused by generic exception wrapping.

Why

  • Align async streaming behavior with the rest of the SDK’s error model.
  • Keep failures actionable for SDK users in real integrations.

Testing

  • pytest -q tests/test_research.py
  • pytest -q

Note

Medium Risk
Changes exception propagation in async streaming mode, which can affect downstream error handling, but is limited in scope and covered by a targeted regression test.

Overview
Async research(..., stream=True) now preserves Tavily-specific error classes (UsageLimitExceededError, ForbiddenError, InvalidAPIKeyError, BadRequestError, TimeoutError) instead of catching them and rethrowing a generic Exception; unexpected errors are still wrapped but now use exception chaining (raise ... from e).

Adds a regression test ensuring a 429 response in streaming mode raises UsageLimitExceededError with the server-provided message.

Reviewed by Cursor Bugbot for commit 9958f47. Bugbot is set up for automated code reviews on this repo. Configure here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant