Skip to content

Commit 3ab9179

Browse files
committed
Record finish reasons on generation spans
1 parent ce897b9 commit 3ab9179

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/span_processor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,20 @@ def _get_attributes_from_generation_span_data(
16181618
if hasattr(span_data, "data_source_id"):
16191619
yield GEN_AI_DATA_SOURCE_ID, span_data.data_source_id
16201620

1621+
finish_reasons: list[Any] = []
1622+
if span_data.output:
1623+
for part in span_data.output:
1624+
if isinstance(part, dict):
1625+
fr = part.get("finish_reason") or part.get("stop_reason")
1626+
else:
1627+
fr = getattr(part, "finish_reason", None)
1628+
if fr:
1629+
finish_reasons.append(
1630+
fr if isinstance(fr, str) else str(fr)
1631+
)
1632+
if finish_reasons:
1633+
yield GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons
1634+
16211635
# Usage information
16221636
if span_data.usage:
16231637
usage = span_data.usage

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/tests/test_z_span_processor_unit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def test_attribute_builders(processor_setup):
272272
assert gen_attrs[ServerAttributes.SERVER_PORT] == 443
273273
assert gen_attrs[sp.GEN_AI_USAGE_INPUT_TOKENS] == 10
274274
assert gen_attrs[sp.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
275+
assert gen_attrs[sp.GEN_AI_RESPONSE_FINISH_REASONS] == ["stop"]
275276
assert json.loads(gen_attrs[sp.GEN_AI_INPUT_MESSAGES])[0]["role"] == "user"
276277
assert (
277278
json.loads(gen_attrs[sp.GEN_AI_OUTPUT_MESSAGES])[0]["role"]
@@ -306,6 +307,7 @@ def __init__(self) -> None:
306307
)
307308
assert response_attrs[sp.GEN_AI_RESPONSE_ID] == "resp-1"
308309
assert response_attrs[sp.GEN_AI_RESPONSE_MODEL] == "gpt-4o"
310+
assert response_attrs[sp.GEN_AI_RESPONSE_FINISH_REASONS] == ["stop"]
309311
assert response_attrs[sp.GEN_AI_USAGE_INPUT_TOKENS] == 7
310312
assert response_attrs[sp.GEN_AI_USAGE_OUTPUT_TOKENS] == 2
311313
assert response_attrs[sp.GEN_AI_OUTPUT_TYPE] == sp.GenAIOutputType.TEXT

0 commit comments

Comments
 (0)