langfuse-python icon indicating copy to clipboard operation
langfuse-python copied to clipboard

Update openai.py to handle where OpenAI Agents SDK sends an Omit type

Open druce opened this issue 4 months ago • 5 comments

When using OpenAI Agents SDK, an Omit sentinel can be sent, resulting in raising the "metadata must be a dictionary" error


[!IMPORTANT] Adds handling for Omit type in metadata validation in openai.py to prevent TypeError with OpenAI Agents SDK.

  • Behavior:
    • Adds handling for Omit type in metadata validation in _get_langfuse_data_from_kwargs() in openai.py.
    • Prevents TypeError when OpenAI Agents SDK sends Omit sentinel.
  • Imports:
    • Imports Omit from openai._types.
  • Misc:
    • Mirrors existing NotGiven handling pattern.

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


Disclaimer: Experimental PR review

Greptile Overview

Updated On: 2025-11-03 18:01:51 UTC

Greptile Summary

Adds handling for OpenAI's Omit sentinel type in metadata validation to prevent TypeError when using the OpenAI Agents SDK.

Key changes:

  • Imports Omit type from openai._types alongside existing NotGiven import
  • Adds isinstance(metadata, Omit) check in _get_langfuse_data_from_kwargs function at langfuse/openai.py:401

Context: This mirrors a previous fix (commit 9c6a71d) that added NotGiven handling. The OpenAI Agents SDK can send Omit sentinel values for optional parameters, which was causing the "metadata must be a dictionary" validation error to be raised incorrectly. The fix follows the established pattern and allows Omit values to pass through validation just like NotGiven values.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk
  • The change is a straightforward fix that adds Omit type handling to match the existing NotGiven pattern. It directly addresses the reported issue without introducing new logic. Score of 4 (not 5) due to lack of test coverage for the new Omit handling, though the change follows the established pattern.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
langfuse/openai.py 4/5 Added Omit type handling to metadata validation, mirroring existing NotGiven check to prevent TypeError when OpenAI Agents SDK passes Omit sentinel

Sequence Diagram

sequenceDiagram
    participant AgentsSDK as OpenAI Agents SDK
    participant LangfuseOpenAI as langfuse.openai
    participant Validator as _get_langfuse_data_from_kwargs
    participant Client as Langfuse Client

    AgentsSDK->>LangfuseOpenAI: API call with metadata=Omit
    LangfuseOpenAI->>Validator: Extract kwargs
    Validator->>Validator: Check metadata type
    alt metadata is Omit
        Validator->>Validator: Skip validation (Omit sentinel)
    else metadata is NotGiven
        Validator->>Validator: Skip validation (NotGiven sentinel)
    else metadata is dict
        Validator->>Validator: Validation passes
    else metadata is other type
        Validator-->>LangfuseOpenAI: Raise TypeError
    end
    Validator->>Client: Pass validated metadata
    Client->>Client: Process trace/observation

druce avatar Nov 03 '25 16:11 druce

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 03 '25 16:11 CLAassistant

@druce Thanks a lot for your contribution! Did you check when Omit was added to the OpenAI SDK so we can avoid missing import errors on usage with older OpenAI SDK versions?

hassiebp avatar Nov 03 '25 17:11 hassiebp

line 328 here is where openai sends an Omit object - https://github.com/openai/openai-agents-python/blob/main/src/agents/models/openai_responses.py

to replicate: in environment set OPENAI_API_KEY, LANGFUSE_TRACING_ENABLED=true, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_HOST run test.py:

import asyncio
from agents import Agent, Runner, set_default_openai_client
from langfuse.openai import AsyncOpenAI  # Use Langfuse-wrapped client


class SimpleAgent(Agent):
    def __init__(self):
        super().__init__(name="Test", model="gpt-4o-mini", instructions="You are helpful")


async def main():
    # This client is wrapped by Langfuse
    set_default_openai_client(AsyncOpenAI())
    result = await Runner.run(SimpleAgent(), "Hello")
    print("Success:", result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

druce avatar Nov 03 '25 17:11 druce

inadvertently clicked 'close with comment!'

druce avatar Nov 03 '25 17:11 druce

@druce Thanks a lot for your contribution! Did you check when Omit was added to the OpenAI SDK so we can avoid missing import errors on usage with older OpenAI SDK versions?

I think it was here?

https://github.com/openai/openai-agents-python/commit/94077432b1b7fd3c2bc0c1bb403517f2a79d15c1

druce avatar Nov 03 '25 18:11 druce