Skip to content

Commit 96b2bf8

Browse files
authored
fix(openai): handle pydantic BaseModel as metadata (#1449)
1 parent ccb41c0 commit 96b2bf8

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

langfuse/openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,10 @@ def _get_langfuse_data_from_kwargs(resource: OpenAiDefinition, kwargs: Any) -> A
400400
and not isinstance(metadata, NotGiven)
401401
and not isinstance(metadata, dict)
402402
):
403-
raise TypeError("metadata must be a dictionary")
403+
if isinstance(metadata, BaseModel):
404+
metadata = metadata.model_dump()
405+
else:
406+
metadata = {}
404407

405408
model = kwargs.get("model", None) or None
406409

tests/test_openai.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,16 +607,6 @@ def test_fails_wrong_name(openai):
607607
)
608608

609609

610-
def test_fails_wrong_metadata(openai):
611-
with pytest.raises(TypeError, match="metadata must be a dictionary"):
612-
openai.OpenAI().completions.create(
613-
metadata="metadata",
614-
model="gpt-3.5-turbo-instruct",
615-
prompt="1 + 1 = ",
616-
temperature=0,
617-
)
618-
619-
620610
def test_fails_wrong_trace_id(openai):
621611
with pytest.raises(TypeError, match="trace_id must be a string"):
622612
openai.OpenAI().completions.create(

0 commit comments

Comments
 (0)