portkey-python-sdk icon indicating copy to clipboard operation
portkey-python-sdk copied to clipboard

fix: use external openai NotGiven/Omit types for compatibility

Open roh26it opened this issue 1 week ago • 0 comments

Problem

When using Portkey with libraries like pydantic-ai that pass NOT_GIVEN values from the standard openai package, users encounter:

TypeError: Object of type NotGiven is not JSON serializable

This happens because Portkey's vendored OpenAI SDK defines its own NotGiven class, which is a different class from the standard openai's NotGiven. When pydantic-ai passes values using the standard openai's NOT_GIVEN, the vendored code doesn't recognize them as NotGiven instances.

Solution

Instead of defining duplicate NotGiven and Omit classes in the vendored code, we now:

  1. Try to import from the external openai package if it's installed
  2. Fall back to local definitions only if openai is not installed

This ensures that when libraries like pydantic-ai pass NOT_GIVEN values from the standard openai package, Portkey's vendored SDK recognizes them because they're the exact same class.

Testing

import openai._types as external_types
import portkey_ai._vendor.openai._types as vendored_types

# Now they're the same class
assert external_types.NotGiven is vendored_types.NotGiven  # ✓
assert external_types.NOT_GIVEN is vendored_types.NOT_GIVEN  # ✓

# isinstance checks work correctly
from portkey_ai._vendor.openai._utils import is_given, strip_not_given
assert is_given(external_types.NOT_GIVEN) == False  # ✓
assert strip_not_given({'a': 1, 'b': external_types.NOT_GIVEN}) == {'a': 1}  # ✓

roh26it avatar Nov 26 '25 01:11 roh26it