diff --git a/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/__init__.py b/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/__init__.py index 7a6076666..ba9b42071 100644 --- a/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/__init__.py +++ b/packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/shared/__init__.py @@ -166,5 +166,9 @@ def is_streaming_response(response): def model_as_dict(model): if version("pydantic") < "2.0.0": return model.dict() - - return model.model_dump() + if hasattr(model, "model_dump"): + return model.model_dump() + elif hasattr(model, "parse"): # Raw API response + return model_as_dict(model.parse()) + else: + return model diff --git a/packages/opentelemetry-instrumentation-openai/tests/test_embeddings.py b/packages/opentelemetry-instrumentation-openai/tests/test_embeddings.py index 583e5b87c..add12d419 100644 --- a/packages/opentelemetry-instrumentation-openai/tests/test_embeddings.py +++ b/packages/opentelemetry-instrumentation-openai/tests/test_embeddings.py @@ -25,7 +25,7 @@ def test_embeddings(exporter, openai_client): @pytest.mark.vcr def test_embeddings_with_raw_response(exporter, openai_client): - openai_client.embeddings.with_raw_response.create( + response = openai_client.embeddings.with_raw_response.create( input="Tell me a joke about opentelemetry", model="text-embedding-ada-002", ) @@ -39,6 +39,13 @@ def test_embeddings_with_raw_response(exporter, openai_client): == "Tell me a joke about opentelemetry" ) + assert open_ai_span.attributes["llm.request.model"] == "text-embedding-ada-002" + assert open_ai_span.attributes["llm.usage.prompt_tokens"] == 8 + assert open_ai_span.attributes["openai.api_base"] == "https://api.openai.com/v1/" + + parsed_response = response.parse() + assert parsed_response.data[0] + @pytest.mark.vcr def test_azure_openai_embeddings(exporter):