Skip to content

Commit

Permalink
Fixes traceloop#535 by validating for LegacyAPIResponse or anything w…
Browse files Browse the repository at this point in the history
…hich has the .parse() method and then recursively calling the result so that span attributes are still captured
  • Loading branch information
tonybaloney committed Feb 27, 2024
1 parent b065c03 commit 2c0dd0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand All @@ -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):
Expand Down

0 comments on commit 2c0dd0c

Please sign in to comment.