Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/openai/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async def _async_transform_recursive(
return data

if isinstance(data, pydantic.BaseModel):
return model_dump(data, exclude_unset=True, mode="json")
return model_dump(data, exclude_unset=True, mode="json", exclude=getattr(data, "__api_exclude__", None))

annotated_type = _get_annotated_type(annotation)
if annotated_type is None:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,21 @@ async def test_pydantic_default_field(use_async: bool) -> None:
assert cast(Any, await transform(model, Any, use_async)) == {"with_none_default": "bar", "with_str_default": "baz"}


class ModelWithApiExcludedField(BaseModel):
foo: str
client_only: Union[str, None] = None

__api_exclude__ = {"client_only"}


@parametrize
@pytest.mark.asyncio
async def test_pydantic_model_api_exclude(use_async: bool) -> None:
model = ModelWithApiExcludedField(foo="hello!", client_only="secret")
assert cast(Any, await transform(model, Any, use_async)) == {"foo": "hello!"}
assert cast(Any, await transform([model], List[ModelWithApiExcludedField], use_async)) == [{"foo": "hello!"}]


class TypedDictIterableUnion(TypedDict):
foo: Annotated[Union[Bar8, Iterable[Baz8]], PropertyInfo(alias="FOO")]

Expand Down