77
88
99class PortkeyJSONEncoder (json .JSONEncoder ):
10- """Custom JSON encoder that handles NotGiven/Omit types using OpenAI's utilities."""
11-
12- def default (self , obj ):
13- # Use OpenAI's is_given utility to check for NotGiven/Omit sentinels
10+ """JSON encoder that treats OpenAI/Portkey "not provided" markers as null."""
11+
12+ def default (self , obj ): # type: ignore[override]
13+ # If this is one of OpenAI's internal "not provided" / omit markers,
14+ # encode it as None (null in JSON) instead of raising TypeError.
1415 if not is_given (obj ):
15- # Return None for NotGiven/Omit instances during JSON serialization
1616 return None
1717 return super ().default (obj )
1818
1919
20- def enable_notgiven_serialization ():
21- """Enable global JSON serialization support for NotGiven/Omit types.
22-
23- Uses OpenAI's is_given utility to detect sentinel values.
20+ def enable_notgiven_serialization () -> None :
21+ """Enable global JSON support for OpenAI/Portkey "not provided" markers.
22+
23+ After this is called (done automatically in portkey_ai.__init__), any
24+ json.dumps(...) that encounters these markers will encode them as null
25+ instead of raising a TypeError.
2426 """
2527 global _patched_notgiven_serialization
2628 if _patched_notgiven_serialization :
2729 return
2830
29- def patched_default (self , obj ):
30- # Use OpenAI's utility to check for NotGiven/Omit
31+ def patched_default (self , obj ): # type: ignore[override]
3132 if not is_given (obj ):
3233 return None
3334 return _BASE_JSON_DEFAULT (self , obj )
@@ -36,10 +37,10 @@ def patched_default(self, obj):
3637 _patched_notgiven_serialization = True
3738
3839
39- def disable_notgiven_serialization ():
40- """Disable global JSON serialization support for NotGiven/Omit types .
41-
42- This restores the original JSONEncoder behavior .
40+ def disable_notgiven_serialization () -> None :
41+ """Disable global JSON support for OpenAI/Portkey "not provided" markers .
42+
43+ Restores the original JSONEncoder.default implementation .
4344 """
4445 global _patched_notgiven_serialization
4546 if not _patched_notgiven_serialization :
0 commit comments