Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Nov 24, 2025

Important

Handle pydantic.BaseModel as metadata in langfuse/openai.py by converting it to a dictionary, and update tests accordingly.

  • Behavior:
    • In _get_langfuse_data_from_kwargs() in langfuse/openai.py, handle metadata as pydantic.BaseModel by converting it to a dictionary using model_dump(). If not a BaseModel or dict, set metadata to an empty dictionary.
  • Tests:
    • Remove test_fails_wrong_metadata() from tests/test_openai.py as metadata no longer raises TypeError for non-dict inputs.

This description was created by Ellipsis for fdc8a2b. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Overview

Greptile Summary

Added support for Pydantic BaseModel as metadata by converting it to dict using model_dump(). However, the implementation has a critical flaw: invalid metadata types (strings, numbers, etc.) are now silently converted to empty dict {} instead of raising a TypeError.

Key Issues:

  • The else branch in the metadata validation (line 406) catches all non-dict, non-BaseModel types and silently converts them to {}
  • This breaks existing error handling behavior - the original code correctly raised TypeError for invalid metadata
  • The removed test test_fails_wrong_metadata verified this error behavior, but no test was added to verify BaseModel handling works correctly

Confidence Score: 1/5

  • This PR is not safe to merge due to a critical logic error in metadata validation
  • Score reflects a logic bug that silently converts invalid metadata to empty dict instead of raising errors, breaking existing validation behavior and removing important error feedback for users
  • langfuse/openai.py requires immediate attention to fix the metadata validation logic

Important Files Changed

File Analysis

Filename Score Overview
langfuse/openai.py 1/5 added BaseModel handling for metadata, but else branch silently converts invalid types to empty dict instead of raising error
tests/test_openai.py 3/5 removed test for invalid metadata, but no test added to verify BaseModel metadata handling works

Sequence Diagram

sequenceDiagram
    participant User
    participant OpenAI Client
    participant _get_langfuse_data_from_kwargs
    participant Langfuse

    User->>OpenAI Client: create(metadata=BaseModel())
    OpenAI Client->>_get_langfuse_data_from_kwargs: kwargs with metadata
    _get_langfuse_data_from_kwargs->>_get_langfuse_data_from_kwargs: Check if metadata is dict
    alt metadata is BaseModel
        _get_langfuse_data_from_kwargs->>_get_langfuse_data_from_kwargs: metadata.model_dump()
    else metadata is invalid type
        _get_langfuse_data_from_kwargs->>_get_langfuse_data_from_kwargs: metadata = {} (silent conversion)
    end
    _get_langfuse_data_from_kwargs->>Langfuse: return langfuse_data with metadata
    Langfuse-->>User: tracked generation
Loading

@hassiebp hassiebp enabled auto-merge (squash) November 24, 2025 09:48
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@hassiebp hassiebp merged commit 96b2bf8 into main Nov 24, 2025
12 checks passed
@hassiebp hassiebp deleted the hassieb/lfe-7577-openai-metadata branch November 24, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: inference cause exception when updating OpenAI agent SDK

2 participants