Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tavily/async_tavily.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,11 @@ async def stream_generator() -> AsyncGenerator[bytes, None]:
yield chunk
except httpx.TimeoutException:
raise TimeoutError(timeout)
except (UsageLimitExceededError, ForbiddenError, InvalidAPIKeyError, BadRequestError, TimeoutError):
# Preserve Tavily-specific error types for callers in streaming mode.
raise
except Exception as e:
raise Exception(f"Error during research stream: {str(e)}")
raise Exception(f"Error during research stream: {str(e)}") from e

return stream_generator()
else:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_research.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import asyncio

import pytest

from tavily.errors import UsageLimitExceededError

BASE_URL = "https://api.tavily.com"

dummy_queued_response = {
Expand Down Expand Up @@ -147,3 +151,19 @@ def test_async_get_research(async_interceptor, async_client):
request = async_interceptor.get_request()
validate_get_research(request, response)


def test_async_research_stream_preserves_usage_limit_error(async_interceptor, async_client):
async_interceptor.set_response(429, json={"detail": {"error": "quota exceeded"}})

async def run_test():
stream = await async_client.research(
input="Research the latest developments in AI",
stream=True,
)

with pytest.raises(UsageLimitExceededError, match="quota exceeded"):
async for _ in stream:
pass

asyncio.run(run_test())