Skip to content

Commit d5243c3

Browse files
authored
Ensure test_openai_agents passes with xdist (#920)
1 parent 7340b5a commit d5243c3

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

tests/otel_integrations/test_openai_agents.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import sys
35
from typing import TYPE_CHECKING, Any
@@ -298,6 +300,26 @@ def test_openai_agent_tracing_manual_start_end(exporter: TestExporter):
298300
)
299301

300302

303+
def without_code_attrs(spans: list[dict[str, Any]]) -> list[dict[str, Any]]:
304+
"""
305+
The Agents SDK runs some things in some weird async context that messes with getting consistent code attributes.
306+
307+
This function removes the code attributes from the spans to make the tests more stable.
308+
It only does this if code.function is missing which is a sign of the problem.
309+
"""
310+
return [
311+
{
312+
**span,
313+
'attributes': {
314+
k: v
315+
for k, v in span['attributes'].items()
316+
if 'code.function' in span['attributes'] or not k.startswith('code')
317+
},
318+
}
319+
for span in spans
320+
]
321+
322+
301323
@pytest.mark.vcr()
302324
@pytest.mark.anyio
303325
async def test_responses(exporter: TestExporter):
@@ -313,7 +335,7 @@ def random_number() -> int:
313335
with logfire.instrument_openai():
314336
await Runner.run(agent1, input='Generate a random number then, hand off to agent2.')
315337

316-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
338+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
317339
[
318340
{
319341
'name': 'Responses API with {gen_ai.request.model!r}',
@@ -322,8 +344,6 @@ def random_number() -> int:
322344
'start_time': 3000000000,
323345
'end_time': 4000000000,
324346
'attributes': {
325-
'code.filepath': IsStr(),
326-
'code.lineno': 123,
327347
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
328348
'logfire.span_type': 'span',
329349
'logfire.msg': "Responses API with 'gpt-4o'",
@@ -529,8 +549,6 @@ def random_number() -> int:
529549
'start_time': 5000000000,
530550
'end_time': 6000000000,
531551
'attributes': {
532-
'code.filepath': IsStr(),
533-
'code.lineno': 123,
534552
'logfire.msg_template': 'Function: {name}',
535553
'logfire.span_type': 'span',
536554
'logfire.msg': 'Function: random_number',
@@ -547,8 +565,6 @@ def random_number() -> int:
547565
'start_time': 7000000000,
548566
'end_time': 8000000000,
549567
'attributes': {
550-
'code.filepath': IsStr(),
551-
'code.lineno': 123,
552568
'logfire.msg_template': 'Handoff: {from_agent} -> {to_agent}',
553569
'logfire.span_type': 'span',
554570
'logfire.msg': 'Handoff: agent1 -> agent2',
@@ -897,7 +913,7 @@ async def zero_guardrail(_context: Any, _agent: Agent[Any], inp: Any) -> Guardra
897913
with pytest.raises(InputGuardrailTripwireTriggered):
898914
await Runner.run(agent, '0?')
899915

900-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
916+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
901917
[
902918
{
903919
'name': 'Guardrail {name!r} {triggered=}',
@@ -906,8 +922,6 @@ async def zero_guardrail(_context: Any, _agent: Agent[Any], inp: Any) -> Guardra
906922
'start_time': 3000000000,
907923
'end_time': 4000000000,
908924
'attributes': {
909-
'code.filepath': IsStr(),
910-
'code.lineno': 123,
911925
'logfire.msg_template': 'Guardrail {name!r} {triggered=}',
912926
'logfire.span_type': 'span',
913927
'logfire.msg': "Guardrail 'zero_guardrail' triggered=False",
@@ -923,8 +937,6 @@ async def zero_guardrail(_context: Any, _agent: Agent[Any], inp: Any) -> Guardra
923937
'start_time': 5000000000,
924938
'end_time': 6000000000,
925939
'attributes': {
926-
'code.filepath': IsStr(),
927-
'code.lineno': 123,
928940
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
929941
'logfire.span_type': 'span',
930942
'logfire.msg': "Responses API with 'gpt-4o'",
@@ -1130,8 +1142,6 @@ async def zero_guardrail(_context: Any, _agent: Agent[Any], inp: Any) -> Guardra
11301142
'start_time': 11000000000,
11311143
'end_time': 12000000000,
11321144
'attributes': {
1133-
'code.filepath': IsStr(),
1134-
'code.lineno': 123,
11351145
'logfire.msg_template': 'Guardrail {name!r} {triggered=}',
11361146
'logfire.span_type': 'span',
11371147
'logfire.msg': "Guardrail 'zero_guardrail' triggered=True",
@@ -1212,7 +1222,7 @@ async def test_chat_completions(exporter: TestExporter):
12121222
agent = Agent[str](name='my_agent', model=model)
12131223
with logfire.instrument_openai():
12141224
await Runner.run(agent, '1+1?')
1215-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
1225+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
12161226
[
12171227
{
12181228
'name': 'Chat completion with {gen_ai.request.model!r}',
@@ -1221,8 +1231,6 @@ async def test_chat_completions(exporter: TestExporter):
12211231
'start_time': 3000000000,
12221232
'end_time': 4000000000,
12231233
'attributes': {
1224-
'code.filepath': IsStr(),
1225-
'code.lineno': 123,
12261234
'logfire.msg_template': 'Chat completion with {gen_ai.request.model!r}',
12271235
'logfire.tags': ('LLM',),
12281236
'logfire.span_type': 'span',
@@ -1483,7 +1491,7 @@ def type(self) -> str:
14831491
}
14841492
)
14851493

1486-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
1494+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
14871495
[
14881496
{
14891497
'name': 'OpenAI agents: {type} span',
@@ -1541,7 +1549,7 @@ async def test_responses_simple(exporter: TestExporter):
15411549
result = await Runner.run(agent1, input='2+2?')
15421550
await Runner.run(agent1, input=result.to_input_list() + [{'role': 'user', 'content': '4?'}])
15431551

1544-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
1552+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
15451553
[
15461554
{
15471555
'name': 'Responses API with {gen_ai.request.model!r}',
@@ -1550,8 +1558,6 @@ async def test_responses_simple(exporter: TestExporter):
15501558
'start_time': 3000000000,
15511559
'end_time': 4000000000,
15521560
'attributes': {
1553-
'code.filepath': IsStr(),
1554-
'code.lineno': 123,
15551561
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
15561562
'logfire.span_type': 'span',
15571563
'logfire.msg': "Responses API with 'gpt-4o'",
@@ -1729,8 +1735,6 @@ async def test_responses_simple(exporter: TestExporter):
17291735
'start_time': 7000000000,
17301736
'end_time': 8000000000,
17311737
'attributes': {
1732-
'code.filepath': IsStr(),
1733-
'code.lineno': 123,
17341738
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
17351739
'logfire.span_type': 'span',
17361740
'logfire.msg': "Responses API with 'gpt-4o'",
@@ -1959,7 +1963,7 @@ async def test_file_search(exporter: TestExporter):
19591963
result = await Runner.run(agent, 'Who made Logfire?')
19601964
await Runner.run(agent, input=result.to_input_list() + [{'role': 'user', 'content': '2+2?'}])
19611965

1962-
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
1966+
assert without_code_attrs(exporter.exported_spans_as_dict(parse_json_attributes=True)) == snapshot(
19631967
[
19641968
{
19651969
'name': 'Responses API with {gen_ai.request.model!r}',
@@ -1968,8 +1972,6 @@ async def test_file_search(exporter: TestExporter):
19681972
'start_time': 3000000000,
19691973
'end_time': 4000000000,
19701974
'attributes': {
1971-
'code.filepath': IsStr(),
1972-
'code.lineno': 123,
19731975
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
19741976
'logfire.span_type': 'span',
19751977
'logfire.msg': "Responses API with 'gpt-4o'",
@@ -2226,8 +2228,6 @@ async def test_file_search(exporter: TestExporter):
22262228
'start_time': 7000000000,
22272229
'end_time': 8000000000,
22282230
'attributes': {
2229-
'code.filepath': IsStr(),
2230-
'code.lineno': 123,
22312231
'logfire.msg_template': 'Responses API with {gen_ai.request.model!r}',
22322232
'logfire.span_type': 'span',
22332233
'logfire.msg': "Responses API with 'gpt-4o'",

0 commit comments

Comments
 (0)