Commit a303acc3 authored by Your Name's avatar Your Name

fix: Fix SDK async streaming - use create(stream=True) instead of stream()

The Anthropic SDK's messages.stream() is a synchronous context manager,
not async. For async streaming, we need to use messages.create(..., stream=True)
which returns an async iterator of ServerSentEvent objects.

Changed from:
    async with client.messages.stream(**request_kwargs) as stream:
To:
    stream = await client.messages.create(**request_kwargs, stream=True)
    async for event in stream:
parent 0ba372d8
...@@ -3932,10 +3932,10 @@ class ClaudeProviderHandler(BaseProviderHandler): ...@@ -3932,10 +3932,10 @@ class ClaudeProviderHandler(BaseProviderHandler):
async def _handle_streaming_request_sdk(self, client, request_kwargs: Dict, model: str): async def _handle_streaming_request_sdk(self, client, request_kwargs: Dict, model: str):
""" """
Handle streaming request using Anthropic SDK. Handle streaming request using Anthropic SDK's async streaming API.
The SDK handles proper streaming event parsing and retries. Uses client.messages.create(..., stream=True) which returns an async iterator
We convert SDK events to OpenAI-compatible SSE chunks. of ServerSentEvent objects. We parse these events and convert to OpenAI SSE chunks.
""" """
import logging import logging
import json import json
...@@ -3961,8 +3961,9 @@ class ClaudeProviderHandler(BaseProviderHandler): ...@@ -3961,8 +3961,9 @@ class ClaudeProviderHandler(BaseProviderHandler):
idle_timeout = self.stream_idle_timeout idle_timeout = self.stream_idle_timeout
try: try:
# Use SDK's streaming API with context manager # Use SDK's async streaming API - create(stream=True) returns async iterator
async with client.messages.stream(**request_kwargs) as stream: stream = await client.messages.create(**request_kwargs, stream=True)
async for event in stream: async for event in stream:
# Update idle watchdog # Update idle watchdog
last_event_time = time.time() last_event_time = time.time()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment