Update openai.py to handle where OpenAI Agents SDK sends an Omit type
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
Omittype in metadata validation inopenai.pyto prevent TypeError with OpenAI Agents SDK.
- Behavior:
- Adds handling for
Omittype in metadata validation in_get_langfuse_data_from_kwargs()inopenai.py.- Prevents TypeError when OpenAI Agents SDK sends
Omitsentinel.- Imports:
- Imports
Omitfromopenai._types.- Misc:
- Mirrors existing
NotGivenhandling pattern.This description was created by
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
Omittype fromopenai._typesalongside existingNotGivenimport - Adds
isinstance(metadata, Omit)check in_get_langfuse_data_from_kwargsfunction 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
Omittype handling to match the existingNotGivenpattern. It directly addresses the reported issue without introducing new logic. Score of 4 (not 5) due to lack of test coverage for the newOmithandling, 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 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?
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())
inadvertently clicked 'close with comment!'
@druce Thanks a lot for your contribution! Did you check when
Omitwas 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