Skip to content

Commit aee29a6

Browse files
Merge branch 'webb/langchain/pipeline-name' into webb/langchain/tool-pipeline-name
2 parents ee0b7d9 + ea94bfc commit aee29a6

3 files changed

Lines changed: 94 additions & 44 deletions

File tree

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,30 @@ def nonstreaming_responses_model_response():
11021102
)
11031103

11041104

1105+
@pytest.fixture
1106+
def nonstreaming_chat_completions_model_response():
1107+
return openai.types.chat.ChatCompletion(
1108+
id="chat-id",
1109+
choices=[
1110+
openai.types.chat.chat_completion.Choice(
1111+
index=0,
1112+
finish_reason="stop",
1113+
message=openai.types.chat.ChatCompletionMessage(
1114+
role="assistant", content="the model response"
1115+
),
1116+
)
1117+
],
1118+
created=10000000,
1119+
model="response-model-id",
1120+
object="chat.completion",
1121+
usage=openai.types.CompletionUsage(
1122+
completion_tokens=10,
1123+
prompt_tokens=20,
1124+
total_tokens=30,
1125+
),
1126+
)
1127+
1128+
11051129
@pytest.fixture
11061130
def responses_tool_call_model_responses():
11071131
def inner(

tests/integrations/langchain/test_langchain.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_langchain_chat(
174174
sentry_init,
175175
capture_events,
176176
get_model_response,
177-
nonstreaming_responses_model_response,
177+
nonstreaming_chat_completions_model_response,
178178
):
179179
sentry_init(
180180
integrations=[
@@ -188,7 +188,7 @@ def test_langchain_chat(
188188
events = capture_events()
189189

190190
model_response = get_model_response(
191-
nonstreaming_responses_model_response,
191+
nonstreaming_chat_completions_model_response,
192192
serialize_pydantic=True,
193193
request_headers={
194194
"X-Stainless-Raw-Response": "True",
@@ -199,7 +199,6 @@ def test_langchain_chat(
199199
model_name="gpt-3.5-turbo",
200200
temperature=0,
201201
openai_api_key="badkey",
202-
use_responses_api=True,
203202
)
204203

205204
with patch.object(

tests/integrations/openai/test_openai.py

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
Omit = None
1616

1717
from openai import AsyncOpenAI, OpenAI, AsyncStream, Stream, OpenAIError
18-
from openai.types import CompletionUsage, CreateEmbeddingResponse, Embedding
19-
from openai.types.chat import ChatCompletion, ChatCompletionMessage, ChatCompletionChunk
20-
from openai.types.chat.chat_completion import Choice
18+
from openai.types import CreateEmbeddingResponse, Embedding
19+
from openai.types.chat import ChatCompletionChunk
2120
from openai.types.chat.chat_completion_chunk import ChoiceDelta, Choice as DeltaChoice
2221
from openai.types.create_embedding_response import Usage as EmbeddingTokenUsage
2322

@@ -60,26 +59,6 @@ async def __call__(self, *args, **kwargs):
6059

6160

6261
OPENAI_VERSION = package_version("openai")
63-
EXAMPLE_CHAT_COMPLETION = ChatCompletion(
64-
id="chat-id",
65-
choices=[
66-
Choice(
67-
index=0,
68-
finish_reason="stop",
69-
message=ChatCompletionMessage(
70-
role="assistant", content="the model response"
71-
),
72-
)
73-
],
74-
created=10000000,
75-
model="response-model-id",
76-
object="chat.completion",
77-
usage=CompletionUsage(
78-
completion_tokens=10,
79-
prompt_tokens=20,
80-
total_tokens=30,
81-
),
82-
)
8362

8463

8564
if SKIP_RESPONSES_TESTS:
@@ -131,7 +110,11 @@ async def __call__(self, *args, **kwargs):
131110
],
132111
)
133112
def test_nonstreaming_chat_completion_no_prompts(
134-
sentry_init, capture_events, send_default_pii, include_prompts
113+
sentry_init,
114+
capture_events,
115+
send_default_pii,
116+
include_prompts,
117+
nonstreaming_chat_completions_model_response,
135118
):
136119
sentry_init(
137120
integrations=[OpenAIIntegration(include_prompts=include_prompts)],
@@ -141,7 +124,9 @@ def test_nonstreaming_chat_completion_no_prompts(
141124
events = capture_events()
142125

143126
client = OpenAI(api_key="z")
144-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
127+
client.chat.completions._post = mock.Mock(
128+
return_value=nonstreaming_chat_completions_model_response
129+
)
145130

146131
with start_transaction(name="openai tx"):
147132
response = (
@@ -228,7 +213,13 @@ def test_nonstreaming_chat_completion_no_prompts(
228213
),
229214
],
230215
)
231-
def test_nonstreaming_chat_completion(sentry_init, capture_events, messages, request):
216+
def test_nonstreaming_chat_completion(
217+
sentry_init,
218+
capture_events,
219+
messages,
220+
request,
221+
nonstreaming_chat_completions_model_response,
222+
):
232223
sentry_init(
233224
integrations=[OpenAIIntegration(include_prompts=True)],
234225
traces_sample_rate=1.0,
@@ -237,7 +228,9 @@ def test_nonstreaming_chat_completion(sentry_init, capture_events, messages, req
237228
events = capture_events()
238229

239230
client = OpenAI(api_key="z")
240-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
231+
client.chat.completions._post = mock.Mock(
232+
return_value=nonstreaming_chat_completions_model_response
233+
)
241234

242235
with start_transaction(name="openai tx"):
243236
response = (
@@ -307,7 +300,11 @@ def test_nonstreaming_chat_completion(sentry_init, capture_events, messages, req
307300
],
308301
)
309302
async def test_nonstreaming_chat_completion_async_no_prompts(
310-
sentry_init, capture_events, send_default_pii, include_prompts
303+
sentry_init,
304+
capture_events,
305+
send_default_pii,
306+
include_prompts,
307+
nonstreaming_chat_completions_model_response,
311308
):
312309
sentry_init(
313310
integrations=[OpenAIIntegration(include_prompts=include_prompts)],
@@ -317,7 +314,9 @@ async def test_nonstreaming_chat_completion_async_no_prompts(
317314
events = capture_events()
318315

319316
client = AsyncOpenAI(api_key="z")
320-
client.chat.completions._post = mock.AsyncMock(return_value=EXAMPLE_CHAT_COMPLETION)
317+
client.chat.completions._post = mock.AsyncMock(
318+
return_value=nonstreaming_chat_completions_model_response
319+
)
321320

322321
with start_transaction(name="openai tx"):
323322
response = await client.chat.completions.create(
@@ -403,7 +402,11 @@ async def test_nonstreaming_chat_completion_async_no_prompts(
403402
],
404403
)
405404
async def test_nonstreaming_chat_completion_async(
406-
sentry_init, capture_events, messages, request
405+
sentry_init,
406+
capture_events,
407+
messages,
408+
request,
409+
nonstreaming_chat_completions_model_response,
407410
):
408411
sentry_init(
409412
integrations=[OpenAIIntegration(include_prompts=True)],
@@ -413,7 +416,9 @@ async def test_nonstreaming_chat_completion_async(
413416
events = capture_events()
414417

415418
client = AsyncOpenAI(api_key="z")
416-
client.chat.completions._post = AsyncMock(return_value=EXAMPLE_CHAT_COMPLETION)
419+
client.chat.completions._post = AsyncMock(
420+
return_value=nonstreaming_chat_completions_model_response
421+
)
417422

418423
with start_transaction(name="openai tx"):
419424
response = await client.chat.completions.create(
@@ -1551,15 +1556,19 @@ async def test_embeddings_create_raises_error_async(
15511556
assert event["level"] == "error"
15521557

15531558

1554-
def test_span_origin_nonstreaming_chat(sentry_init, capture_events):
1559+
def test_span_origin_nonstreaming_chat(
1560+
sentry_init, capture_events, nonstreaming_chat_completions_model_response
1561+
):
15551562
sentry_init(
15561563
integrations=[OpenAIIntegration()],
15571564
traces_sample_rate=1.0,
15581565
)
15591566
events = capture_events()
15601567

15611568
client = OpenAI(api_key="z")
1562-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
1569+
client.chat.completions._post = mock.Mock(
1570+
return_value=nonstreaming_chat_completions_model_response
1571+
)
15631572

15641573
with start_transaction(name="openai tx"):
15651574
client.chat.completions.create(
@@ -1573,15 +1582,19 @@ def test_span_origin_nonstreaming_chat(sentry_init, capture_events):
15731582

15741583

15751584
@pytest.mark.asyncio
1576-
async def test_span_origin_nonstreaming_chat_async(sentry_init, capture_events):
1585+
async def test_span_origin_nonstreaming_chat_async(
1586+
sentry_init, capture_events, nonstreaming_chat_completions_model_response
1587+
):
15771588
sentry_init(
15781589
integrations=[OpenAIIntegration()],
15791590
traces_sample_rate=1.0,
15801591
)
15811592
events = capture_events()
15821593

15831594
client = AsyncOpenAI(api_key="z")
1584-
client.chat.completions._post = AsyncMock(return_value=EXAMPLE_CHAT_COMPLETION)
1595+
client.chat.completions._post = AsyncMock(
1596+
return_value=nonstreaming_chat_completions_model_response
1597+
)
15851598

15861599
with start_transaction(name="openai tx"):
15871600
await client.chat.completions.create(
@@ -3125,15 +3138,19 @@ async def test_streaming_responses_api_async(
31253138
"tools",
31263139
[[], None, NOT_GIVEN, omit],
31273140
)
3128-
def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
3141+
def test_empty_tools_in_chat_completion(
3142+
sentry_init, capture_events, tools, nonstreaming_chat_completions_model_response
3143+
):
31293144
sentry_init(
31303145
integrations=[OpenAIIntegration()],
31313146
traces_sample_rate=1.0,
31323147
)
31333148
events = capture_events()
31343149

31353150
client = OpenAI(api_key="z")
3136-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
3151+
client.chat.completions._post = mock.Mock(
3152+
return_value=nonstreaming_chat_completions_model_response
3153+
)
31373154

31383155
with start_transaction(name="openai tx"):
31393156
client.chat.completions.create(
@@ -3164,7 +3181,11 @@ def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
31643181
],
31653182
)
31663183
def test_openai_message_role_mapping(
3167-
sentry_init, capture_events, test_message, expected_role
3184+
sentry_init,
3185+
capture_events,
3186+
test_message,
3187+
expected_role,
3188+
nonstreaming_chat_completions_model_response,
31683189
):
31693190
"""Test that OpenAI integration properly maps message roles like 'ai' to 'assistant'"""
31703191

@@ -3176,7 +3197,9 @@ def test_openai_message_role_mapping(
31763197
events = capture_events()
31773198

31783199
client = OpenAI(api_key="z")
3179-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
3200+
client.chat.completions._post = mock.Mock(
3201+
return_value=nonstreaming_chat_completions_model_response
3202+
)
31803203

31813204
test_messages = [test_message]
31823205

@@ -3197,7 +3220,9 @@ def test_openai_message_role_mapping(
31973220
assert stored_messages[0]["role"] == expected_role
31983221

31993222

3200-
def test_openai_message_truncation(sentry_init, capture_events):
3223+
def test_openai_message_truncation(
3224+
sentry_init, capture_events, nonstreaming_chat_completions_model_response
3225+
):
32013226
"""Test that large messages are truncated properly in OpenAI integration."""
32023227
sentry_init(
32033228
integrations=[OpenAIIntegration(include_prompts=True)],
@@ -3207,7 +3232,9 @@ def test_openai_message_truncation(sentry_init, capture_events):
32073232
events = capture_events()
32083233

32093234
client = OpenAI(api_key="z")
3210-
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
3235+
client.chat.completions._post = mock.Mock(
3236+
return_value=nonstreaming_chat_completions_model_response
3237+
)
32113238

32123239
large_content = (
32133240
"This is a very long message that will exceed our size limits. " * 1000

0 commit comments

Comments
 (0)