fix: use external openai NotGiven/Omit types for compatibility#415
fix: use external openai NotGiven/Omit types for compatibility#415
Conversation
When libraries like pydantic-ai pass NOT_GIVEN values from the standard openai package, Portkey's vendored SDK now recognizes them because they use the exact same class. This fixes TypeError: Object of type NotGiven is not JSON serializable errors when using pydantic-ai or similar libraries with Portkey.
There was a problem hiding this comment.
Pull request overview
This PR resolves compatibility issues between Portkey's vendored OpenAI SDK and external libraries (like pydantic-ai) that use the standard openai package's NOT_GIVEN sentinel values. The fix prioritizes importing NotGiven, Omit, and NOT_GIVEN from the external openai package when available, falling back to local definitions only when openai is not installed.
Key changes:
- Added try/except block to import
NotGiven,Omit, andNOT_GIVENfrom external openai package - Ensured compatibility by using the same class instances across packages
- Maintained backward compatibility with standalone usage when openai is not installed
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| User code shouldn't need to use not_given directly. | ||
| # External openai only exports NOT_GIVEN, not the lowercase not_given | ||
| not_given = NOT_GIVEN | ||
| # External openai doesn't export lowercase omit, create from class | ||
| omit = Omit() | ||
| except ImportError: | ||
| # Fall back to defining our own (for standalone use without openai installed) |
There was a problem hiding this comment.
Creating a new instance of Omit() in line 127 may not maintain compatibility if the external openai package uses a singleton pattern for omit. Consider checking if the external package exports omit as a module-level constant and using that instead of creating a new instance.
| User code shouldn't need to use not_given directly. | |
| # External openai only exports NOT_GIVEN, not the lowercase not_given | |
| not_given = NOT_GIVEN | |
| # External openai doesn't export lowercase omit, create from class | |
| omit = Omit() | |
| except ImportError: | |
| # Fall back to defining our own (for standalone use without openai installed) | |
| # Try to import the singleton 'omit' if available | |
| try: | |
| from openai._types import omit as _external_omit | |
| omit = _external_omit | |
| except ImportError: | |
| # If not exported, create from class | |
| omit = Omit() | |
| # External openai only exports NOT_GIVEN, not the lowercase not_given | |
| not_given = NOT_GIVEN |
Problem
When using Portkey with libraries like pydantic-ai that pass
NOT_GIVENvalues from the standard openai package, users encounter:This happens because Portkey's vendored OpenAI SDK defines its own
NotGivenclass, which is a different class from the standard openai'sNotGiven. When pydantic-ai passes values using the standard openai'sNOT_GIVEN, the vendored code doesn't recognize them asNotGiveninstances.Solution
Instead of defining duplicate
NotGivenandOmitclasses in the vendored code, we now:This ensures that when libraries like pydantic-ai pass
NOT_GIVENvalues from the standard openai package, Portkey's vendored SDK recognizes them because they're the exact same class.Testing