Skip to content

Commit 4dbb706

Browse files
add test
1 parent c1ec7e1 commit 4dbb706

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

tests/integrations/openai/test_openai.py

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,7 +3206,9 @@ def test_openai_message_truncation(sentry_init, capture_events):
32063206

32073207

32083208
# noinspection PyTypeChecker
3209-
def test_streaming_chat_completion_ttft(sentry_init, capture_events):
3209+
def test_streaming_chat_completion_ttft(
3210+
sentry_init, capture_events, get_model_response, server_side_event_chunks
3211+
):
32103212
"""
32113213
Test that streaming chat completions capture time-to-first-token (TTFT).
32123214
"""
@@ -3217,43 +3219,54 @@ def test_streaming_chat_completion_ttft(sentry_init, capture_events):
32173219
events = capture_events()
32183220

32193221
client = OpenAI(api_key="z")
3220-
returned_stream = Stream(cast_to=None, response=None, client=client)
3221-
returned_stream._iterator = [
3222-
ChatCompletionChunk(
3223-
id="1",
3224-
choices=[
3225-
DeltaChoice(
3226-
index=0, delta=ChoiceDelta(content="Hello"), finish_reason=None
3227-
)
3228-
],
3229-
created=100000,
3230-
model="model-id",
3231-
object="chat.completion.chunk",
3232-
),
3233-
ChatCompletionChunk(
3234-
id="1",
3235-
choices=[
3236-
DeltaChoice(
3237-
index=0, delta=ChoiceDelta(content=" world"), finish_reason="stop"
3238-
)
3222+
returned_stream = get_model_response(
3223+
server_side_event_chunks(
3224+
[
3225+
ChatCompletionChunk(
3226+
id="1",
3227+
choices=[
3228+
DeltaChoice(
3229+
index=0,
3230+
delta=ChoiceDelta(content="Hello"),
3231+
finish_reason=None,
3232+
)
3233+
],
3234+
created=100000,
3235+
model="model-id",
3236+
object="chat.completion.chunk",
3237+
),
3238+
ChatCompletionChunk(
3239+
id="1",
3240+
choices=[
3241+
DeltaChoice(
3242+
index=0,
3243+
delta=ChoiceDelta(content=" world"),
3244+
finish_reason="stop",
3245+
)
3246+
],
3247+
created=100000,
3248+
model="model-id",
3249+
object="chat.completion.chunk",
3250+
),
32393251
],
3240-
created=100000,
3241-
model="model-id",
3242-
object="chat.completion.chunk",
3252+
include_event_type=False,
32433253
),
3244-
]
3245-
3246-
client.chat.completions._post = mock.Mock(return_value=returned_stream)
3254+
)
32473255

3248-
with start_transaction(name="openai tx"):
3249-
response_stream = client.chat.completions.create(
3250-
model="some-model",
3251-
messages=[{"role": "user", "content": "Say hello"}],
3252-
stream=True,
3253-
)
3254-
# Consume the stream
3255-
for _ in response_stream:
3256-
pass
3256+
with mock.patch.object(
3257+
client.chat._client._client,
3258+
"send",
3259+
return_value=returned_stream,
3260+
):
3261+
with start_transaction(name="openai tx"):
3262+
response_stream = client.chat.completions.create(
3263+
model="some-model",
3264+
messages=[{"role": "user", "content": "Say hello"}],
3265+
stream=True,
3266+
)
3267+
# Consume the stream
3268+
for _ in response_stream:
3269+
pass
32573270

32583271
(tx,) = events
32593272
span = tx["spans"][0]

0 commit comments

Comments
 (0)