Skip to content

Commit 196c2fd

Browse files
committed
fix: serialize Pydantic models in request body
Issue: - ExtraInfo class instance submitted as the extra_info parameter is not serialized and results in: "TypeError: Object of type ExtraInfo is not JSON serializable" This fix will: - Serialize BaseModel instances. - Exclude None values (automatically populated for defined model properties and rejected by the API) This will allow users to pass as extra_info parameter: - ExtraInfo instance (as hinted) - Dictionary
1 parent a71d1fa commit 196c2fd

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/crowdstrike_aidr/services/ai_guard.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Literal
55

66
import httpx
7+
from pydantic import BaseModel
78

89
from .._client import SyncAPIClient, make_request_options
910
from .._types import Body, Headers, NotGiven, Omit, Query, not_given, omit
@@ -12,7 +13,15 @@
1213

1314

1415
def _transform_typeddict(data: Mapping[str, object]) -> Mapping[str, object]:
15-
return {key: value for key, value in data.items() if is_given(value)}
16+
result = {}
17+
for key, value in data.items():
18+
if is_given(value):
19+
# Serialize Pydantic models to dict, excluding None values
20+
if isinstance(value, BaseModel):
21+
result[key] = value.model_dump(exclude_none=True)
22+
else:
23+
result[key] = value
24+
return result
1625

1726

1827
class AIGuard(SyncAPIClient):

0 commit comments

Comments
 (0)