Skip to content

Commit 6a630a0

Browse files
committed
openai: Use json instead of json_schema and json_object for gen_ai.output.type attribute value
1 parent 6bad350 commit 6a630a0

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,16 @@ def _is_set(value):
166166
# response_format may be string or object with a string in the `type` key
167167
if isinstance(response_format, Mapping):
168168
if _is_set(response_format_type := response_format.get("type")):
169-
span_attributes[GEN_AI_OUTPUT_TYPE] = response_format_type
169+
if response_format_type in ("json_object", "json_schema"):
170+
span_attributes[GEN_AI_OUTPUT_TYPE] = "json"
171+
else:
172+
span_attributes[GEN_AI_OUTPUT_TYPE] = response_format_type
170173
elif isinstance(response_format, str):
171174
span_attributes[GEN_AI_OUTPUT_TYPE] = response_format
172175
else:
173176
# Assume structured output lazily parsed to a schema via type_to_response_format_param or similar.
174177
# e.g. pydantic._internal._model_construction.ModelMetaclass
175-
span_attributes[GEN_AI_OUTPUT_TYPE] = "json_schema"
178+
span_attributes[GEN_AI_OUTPUT_TYPE] = "json"
176179

177180
return span_attributes
178181

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_beta_chat_completions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ def test_parse_response_format_json_object_with_capture_message_content(
15451545
address, port = address_and_port(client)
15461546
assert dict(span.attributes) == {
15471547
GEN_AI_OPENAI_RESPONSE_SERVICE_TIER: "default",
1548-
GEN_AI_OUTPUT_TYPE: "json_object",
1548+
GEN_AI_OUTPUT_TYPE: "json",
15491549
GEN_AI_OPERATION_NAME: "chat",
15501550
GEN_AI_REQUEST_MODEL: TEST_CHAT_MODEL,
15511551
GEN_AI_SYSTEM: "openai",
@@ -1620,7 +1620,7 @@ def test_parse_response_format_structured_output_with_capture_message_content(
16201620
address, port = address_and_port(client)
16211621
assert dict(span.attributes) == {
16221622
GEN_AI_OPENAI_RESPONSE_SERVICE_TIER: "default",
1623-
GEN_AI_OUTPUT_TYPE: "json_schema",
1623+
GEN_AI_OUTPUT_TYPE: "json",
16241624
GEN_AI_OPERATION_NAME: "chat",
16251625
GEN_AI_REQUEST_MODEL: TEST_CHAT_MODEL,
16261626
GEN_AI_SYSTEM: "openai",

0 commit comments

Comments
 (0)