From c614988a725af87ac93d2a01d9f0989936a1b09a Mon Sep 17 00:00:00 2001 From: Konstantin Lapine Date: Fri, 2 Jan 2026 13:21:04 -0800 Subject: [PATCH] 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 --- src/crowdstrike_aidr/services/ai_guard.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/crowdstrike_aidr/services/ai_guard.py b/src/crowdstrike_aidr/services/ai_guard.py index 6972160..a70e056 100644 --- a/src/crowdstrike_aidr/services/ai_guard.py +++ b/src/crowdstrike_aidr/services/ai_guard.py @@ -1,9 +1,10 @@ from __future__ import annotations from collections.abc import Mapping -from typing import Literal +from typing import Any, Literal import httpx +from pydantic import BaseModel from .._client import SyncAPIClient, make_request_options from .._types import Body, Headers, NotGiven, Omit, Query, not_given, omit @@ -11,8 +12,16 @@ from ..models.ai_guard import ExtraInfo, GuardChatCompletionsResponse -def _transform_typeddict(data: Mapping[str, object]) -> Mapping[str, object]: - return {key: value for key, value in data.items() if is_given(value)} +def _transform_typeddict(data: Mapping[str, object]) -> dict[str, Any]: + result: dict[str, Any] = {} + for key, value in data.items(): + if is_given(value): + # Serialize Pydantic models to dict, excluding None values + if isinstance(value, BaseModel): + result[key] = value.model_dump(exclude_none=True) + else: + result[key] = value + return result class AIGuard(SyncAPIClient):