@@ -3269,7 +3269,11 @@ def test_streaming_chat_completion_ttft(sentry_init, capture_events):
32693269# noinspection PyTypeChecker
32703270@pytest .mark .asyncio
32713271async def test_streaming_chat_completion_ttft_async (
3272- sentry_init , capture_events , async_iterator
3272+ sentry_init ,
3273+ capture_events ,
3274+ get_model_response ,
3275+ async_iterator ,
3276+ server_side_event_chunks ,
32733277):
32743278 """
32753279 Test that async streaming chat completions capture time-to-first-token (TTFT).
@@ -3281,47 +3285,56 @@ async def test_streaming_chat_completion_ttft_async(
32813285 events = capture_events ()
32823286
32833287 client = AsyncOpenAI (api_key = "z" )
3284- returned_stream = AsyncStream (cast_to = None , response = None , client = client )
3285- returned_stream ._iterator = async_iterator (
3286- [
3287- ChatCompletionChunk (
3288- id = "1" ,
3289- choices = [
3290- DeltaChoice (
3291- index = 0 , delta = ChoiceDelta (content = "Hello" ), finish_reason = None
3292- )
3293- ],
3294- created = 100000 ,
3295- model = "model-id" ,
3296- object = "chat.completion.chunk" ,
3297- ),
3298- ChatCompletionChunk (
3299- id = "1" ,
3300- choices = [
3301- DeltaChoice (
3302- index = 0 ,
3303- delta = ChoiceDelta (content = " world" ),
3304- finish_reason = "stop" ,
3305- )
3288+ returned_stream = get_model_response (
3289+ async_iterator (
3290+ server_side_event_chunks (
3291+ [
3292+ ChatCompletionChunk (
3293+ id = "1" ,
3294+ choices = [
3295+ DeltaChoice (
3296+ index = 0 ,
3297+ delta = ChoiceDelta (content = "Hello" ),
3298+ finish_reason = None ,
3299+ )
3300+ ],
3301+ created = 100000 ,
3302+ model = "model-id" ,
3303+ object = "chat.completion.chunk" ,
3304+ ),
3305+ ChatCompletionChunk (
3306+ id = "1" ,
3307+ choices = [
3308+ DeltaChoice (
3309+ index = 0 ,
3310+ delta = ChoiceDelta (content = " world" ),
3311+ finish_reason = "stop" ,
3312+ )
3313+ ],
3314+ created = 100000 ,
3315+ model = "model-id" ,
3316+ object = "chat.completion.chunk" ,
3317+ ),
33063318 ],
3307- created = 100000 ,
3308- model = "model-id" ,
3309- object = "chat.completion.chunk" ,
3319+ include_event_type = False ,
33103320 ),
3311- ]
3321+ )
33123322 )
33133323
3314- client .chat .completions ._post = AsyncMock (return_value = returned_stream )
3315-
3316- with start_transaction (name = "openai tx" ):
3317- response_stream = await client .chat .completions .create (
3318- model = "some-model" ,
3319- messages = [{"role" : "user" , "content" : "Say hello" }],
3320- stream = True ,
3321- )
3322- # Consume the stream
3323- async for _ in response_stream :
3324- pass
3324+ with mock .patch .object (
3325+ client .chat ._client ._client ,
3326+ "send" ,
3327+ return_value = returned_stream ,
3328+ ):
3329+ with start_transaction (name = "openai tx" ):
3330+ response_stream = await client .chat .completions .create (
3331+ model = "some-model" ,
3332+ messages = [{"role" : "user" , "content" : "Say hello" }],
3333+ stream = True ,
3334+ )
3335+ # Consume the stream
3336+ async for _ in response_stream :
3337+ pass
33253338
33263339 (tx ,) = events
33273340 span = tx ["spans" ][0 ]
0 commit comments