Skip to content

Commit 540f0f8

Browse files
fix(compat): update signatures of model_dump and model_dump_json for Pydantic v1
1 parent bd2a137 commit 540f0f8

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/anthropic/_models.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,32 +273,41 @@ def model_dump(
273273
mode: Literal["json", "python"] | str = "python",
274274
include: IncEx | None = None,
275275
exclude: IncEx | None = None,
276+
context: Any | None = None,
276277
by_alias: bool | None = None,
277278
exclude_unset: bool = False,
278279
exclude_defaults: bool = False,
279280
exclude_none: bool = False,
281+
exclude_computed_fields: bool = False,
280282
round_trip: bool = False,
281283
warnings: bool | Literal["none", "warn", "error"] = True,
282-
context: dict[str, Any] | None = None,
283-
serialize_as_any: bool = False,
284284
fallback: Callable[[Any], Any] | None = None,
285+
serialize_as_any: bool = False,
285286
) -> dict[str, Any]:
286287
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
287288
288289
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
289290
290291
Args:
291292
mode: The mode in which `to_python` should run.
292-
If mode is 'json', the dictionary will only contain JSON serializable types.
293-
If mode is 'python', the dictionary may contain any Python objects.
294-
include: A list of fields to include in the output.
295-
exclude: A list of fields to exclude from the output.
293+
If mode is 'json', the output will only contain JSON serializable types.
294+
If mode is 'python', the output may contain non-JSON-serializable Python objects.
295+
include: A set of fields to include in the output.
296+
exclude: A set of fields to exclude from the output.
297+
context: Additional context to pass to the serializer.
296298
by_alias: Whether to use the field's alias in the dictionary key if defined.
297-
exclude_unset: Whether to exclude fields that are unset or None from the output.
298-
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
299-
exclude_none: Whether to exclude fields that have a value of `None` from the output.
300-
round_trip: Whether to enable serialization and deserialization round-trip support.
301-
warnings: Whether to log warnings when invalid fields are encountered.
299+
exclude_unset: Whether to exclude fields that have not been explicitly set.
300+
exclude_defaults: Whether to exclude fields that are set to their default value.
301+
exclude_none: Whether to exclude fields that have a value of `None`.
302+
exclude_computed_fields: Whether to exclude computed fields.
303+
While this can be useful for round-tripping, it is usually recommended to use the dedicated
304+
`round_trip` parameter instead.
305+
round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
306+
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
307+
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
308+
fallback: A function to call when an unknown value is encountered. If not provided,
309+
a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.
310+
serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
302311
303312
Returns:
304313
A dictionary representation of the model.
@@ -315,6 +324,8 @@ def model_dump(
315324
raise ValueError("serialize_as_any is only supported in Pydantic v2")
316325
if fallback is not None:
317326
raise ValueError("fallback is only supported in Pydantic v2")
327+
if exclude_computed_fields != False:
328+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
318329
dumped = super().dict( # pyright: ignore[reportDeprecated]
319330
include=include,
320331
exclude=exclude,
@@ -331,15 +342,17 @@ def model_dump_json(
331342
self,
332343
*,
333344
indent: int | None = None,
345+
ensure_ascii: bool = False,
334346
include: IncEx | None = None,
335347
exclude: IncEx | None = None,
348+
context: Any | None = None,
336349
by_alias: bool | None = None,
337350
exclude_unset: bool = False,
338351
exclude_defaults: bool = False,
339352
exclude_none: bool = False,
353+
exclude_computed_fields: bool = False,
340354
round_trip: bool = False,
341355
warnings: bool | Literal["none", "warn", "error"] = True,
342-
context: dict[str, Any] | None = None,
343356
fallback: Callable[[Any], Any] | None = None,
344357
serialize_as_any: bool = False,
345358
) -> str:
@@ -371,6 +384,10 @@ def model_dump_json(
371384
raise ValueError("serialize_as_any is only supported in Pydantic v2")
372385
if fallback is not None:
373386
raise ValueError("fallback is only supported in Pydantic v2")
387+
if ensure_ascii != False:
388+
raise ValueError("ensure_ascii is only supported in Pydantic v2")
389+
if exclude_computed_fields != False:
390+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
374391
return super().json( # type: ignore[reportDeprecated]
375392
indent=indent,
376393
include=include,

0 commit comments

Comments
 (0)