Skip to content

Commit a6b67c5

Browse files
update
1 parent 95a317f commit a6b67c5

2 files changed

Lines changed: 12 additions & 24 deletions

File tree

portkey_ai/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@
169169
enable_notgiven_serialization,
170170
disable_notgiven_serialization,
171171
)
172-
173-
# Automatically enable NotGiven serialization. Users can call disable_notgiven_serialization() if needed.
172+
174173
enable_notgiven_serialization()
175174

176175
api_key = os.environ.get(PORTKEY_API_KEY_ENV)

portkey_ai/utils/json_utils.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ def default(self, obj): # type: ignore[override]
1818

1919

2020
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.
26-
"""
21+
"""Globally encode NotGiven / Omit markers as null in json.dumps."""
2722
global _patched_notgiven_serialization
2823
if _patched_notgiven_serialization:
2924
return
@@ -38,10 +33,7 @@ def patched_default(self, obj): # type: ignore[override]
3833

3934

4035
def disable_notgiven_serialization() -> None:
41-
"""Disable global JSON support for OpenAI/Portkey "not provided" markers.
42-
43-
Restores the original JSONEncoder.default implementation.
44-
"""
36+
"""Restore the original json.JSONEncoder.default implementation."""
4537
global _patched_notgiven_serialization
4638
if not _patched_notgiven_serialization:
4739
return
@@ -51,25 +43,22 @@ def disable_notgiven_serialization() -> None:
5143

5244

5345
def _is_serializable(value) -> bool:
54-
"""Return True if value can be serialized with PortkeyJSONEncoder."""
5546
try:
5647
json.dumps(value, cls=PortkeyJSONEncoder)
57-
return True
5848
except (TypeError, ValueError):
5949
return False
50+
return True
6051

6152

6253
def serialize_kwargs(**kwargs):
63-
# Filter out non-serializable items
64-
serializable_kwargs = {k: v for k, v in kwargs.items() if _is_serializable(v)}
65-
66-
# Convert to string representation
67-
return json.dumps(serializable_kwargs, cls=PortkeyJSONEncoder)
54+
return json.dumps(
55+
{k: v for k, v in kwargs.items() if _is_serializable(v)},
56+
cls=PortkeyJSONEncoder,
57+
)
6858

6959

7060
def serialize_args(*args):
71-
# Filter out non-serializable items
72-
serializable_args = [arg for arg in args if _is_serializable(arg)]
73-
74-
# Convert to string representation
75-
return json.dumps(serializable_args, cls=PortkeyJSONEncoder)
61+
return json.dumps(
62+
[arg for arg in args if _is_serializable(arg)],
63+
cls=PortkeyJSONEncoder,
64+
)

0 commit comments

Comments
 (0)