Skip to content

Commit c7bd234

Browse files
fix(compat): update signatures of model_dump and model_dump_json for Pydantic v1
1 parent 650be39 commit c7bd234

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/openai/_models.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,32 +282,41 @@ def model_dump(
282282
mode: Literal["json", "python"] | str = "python",
283283
include: IncEx | None = None,
284284
exclude: IncEx | None = None,
285+
context: Any | None = None,
285286
by_alias: bool | None = None,
286287
exclude_unset: bool = False,
287288
exclude_defaults: bool = False,
288289
exclude_none: bool = False,
290+
exclude_computed_fields: bool = False,
289291
round_trip: bool = False,
290292
warnings: bool | Literal["none", "warn", "error"] = True,
291-
context: dict[str, Any] | None = None,
292-
serialize_as_any: bool = False,
293293
fallback: Callable[[Any], Any] | None = None,
294+
serialize_as_any: bool = False,
294295
) -> dict[str, Any]:
295296
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
296297
297298
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
298299
299300
Args:
300301
mode: The mode in which `to_python` should run.
301-
If mode is 'json', the dictionary will only contain JSON serializable types.
302-
If mode is 'python', the dictionary may contain any Python objects.
303-
include: A list of fields to include in the output.
304-
exclude: A list of fields to exclude from the output.
302+
If mode is 'json', the output will only contain JSON serializable types.
303+
If mode is 'python', the output may contain non-JSON-serializable Python objects.
304+
include: A set of fields to include in the output.
305+
exclude: A set of fields to exclude from the output.
306+
context: Additional context to pass to the serializer.
305307
by_alias: Whether to use the field's alias in the dictionary key if defined.
306-
exclude_unset: Whether to exclude fields that are unset or None from the output.
307-
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
308-
exclude_none: Whether to exclude fields that have a value of `None` from the output.
309-
round_trip: Whether to enable serialization and deserialization round-trip support.
310-
warnings: Whether to log warnings when invalid fields are encountered.
308+
exclude_unset: Whether to exclude fields that have not been explicitly set.
309+
exclude_defaults: Whether to exclude fields that are set to their default value.
310+
exclude_none: Whether to exclude fields that have a value of `None`.
311+
exclude_computed_fields: Whether to exclude computed fields.
312+
While this can be useful for round-tripping, it is usually recommended to use the dedicated
313+
`round_trip` parameter instead.
314+
round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
315+
warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
316+
"error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
317+
fallback: A function to call when an unknown value is encountered. If not provided,
318+
a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.
319+
serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
311320
312321
Returns:
313322
A dictionary representation of the model.
@@ -324,6 +333,8 @@ def model_dump(
324333
raise ValueError("serialize_as_any is only supported in Pydantic v2")
325334
if fallback is not None:
326335
raise ValueError("fallback is only supported in Pydantic v2")
336+
if exclude_computed_fields != False:
337+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
327338
dumped = super().dict( # pyright: ignore[reportDeprecated]
328339
include=include,
329340
exclude=exclude,
@@ -340,15 +351,17 @@ def model_dump_json(
340351
self,
341352
*,
342353
indent: int | None = None,
354+
ensure_ascii: bool = False,
343355
include: IncEx | None = None,
344356
exclude: IncEx | None = None,
357+
context: Any | None = None,
345358
by_alias: bool | None = None,
346359
exclude_unset: bool = False,
347360
exclude_defaults: bool = False,
348361
exclude_none: bool = False,
362+
exclude_computed_fields: bool = False,
349363
round_trip: bool = False,
350364
warnings: bool | Literal["none", "warn", "error"] = True,
351-
context: dict[str, Any] | None = None,
352365
fallback: Callable[[Any], Any] | None = None,
353366
serialize_as_any: bool = False,
354367
) -> str:
@@ -380,6 +393,10 @@ def model_dump_json(
380393
raise ValueError("serialize_as_any is only supported in Pydantic v2")
381394
if fallback is not None:
382395
raise ValueError("fallback is only supported in Pydantic v2")
396+
if ensure_ascii != False:
397+
raise ValueError("ensure_ascii is only supported in Pydantic v2")
398+
if exclude_computed_fields != False:
399+
raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
383400
return super().json( # type: ignore[reportDeprecated]
384401
indent=indent,
385402
include=include,

0 commit comments

Comments
 (0)