{"record":{"id":"fd3c16d85af05dc2","repo":"openai/openai-python","slug":"serialize-as-any-is-only-supported-in-pydantic-v2","errorCode":null,"errorMessage":"serialize_as_any is only supported in Pydantic v2","messagePattern":"serialize_as_any is only supported in Pydantic v2","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/openai/_models.py","lineNumber":357,"sourceCode":"                warnings: How to handle serialization errors. False/\"none\" ignores them, True/\"warn\" logs errors,\n                    \"error\" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].\n                fallback: A function to call when an unknown value is encountered. If not provided,\n                    a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.\n                serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.\n\n            Returns:\n                A dictionary representation of the model.\n            \"\"\"\n            if mode not in {\"json\", \"python\"}:\n                raise ValueError(\"mode must be either 'json' or 'python'\")\n            if round_trip != False:\n                raise ValueError(\"round_trip is only supported in Pydantic v2\")\n            if warnings != True:\n                raise ValueError(\"warnings is only supported in Pydantic v2\")\n            if context is not None:\n                raise ValueError(\"context is only supported in Pydantic v2\")\n            if serialize_as_any != False:\n                raise ValueError(\"serialize_as_any is only supported in Pydantic v2\")\n            if fallback is not None:\n                raise ValueError(\"fallback is only supported in Pydantic v2\")\n            if exclude_computed_fields != False:\n                raise ValueError(\"exclude_computed_fields is only supported in Pydantic v2\")\n            dumped = super().dict(  # pyright: ignore[reportDeprecated]\n                include=include,\n                exclude=exclude,\n                by_alias=by_alias if by_alias is not None else False,\n                exclude_unset=exclude_unset,\n                exclude_defaults=exclude_defaults,\n                exclude_none=exclude_none,\n            )\n\n            return cast(\"dict[str, Any]\", json_safe(dumped)) if mode == \"json\" else dumped\n\n        @override\n        def model_dump_json(\n            self,","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_models.py#L339-L375","documentation":"serialize_as_any (Pydantic v2 duck-typing serialization, useful for serialized subclass fields declared with a base type) is not implemented in the Pydantic v1 compat shim; only the default False is accepted in model_dump.","triggerScenarios":"Calling model.model_dump(serialize_as_any=True) on SDK models while running the Pydantic v1 implementation — typically to get subclass fields emitted for a field typed as the base model.","commonSituations":"Polymorphic response models where a field is typed as BaseModel but holds a subclass; code copied from Pydantic v2 docs into a pydantic v1 environment.","solutions":["Upgrade to pydantic>=2, where serialize_as_any=True works","Under v1, serialize the object manually (e.g. via .dict() plus custom logic) or restructure the model to include the subclass fields"],"exampleFix":"# before\nd = obj.model_dump(serialize_as_any=True)\n# after (requires pydantic>=2)\n# ensure openai uses pydantic v2, then:\nd = obj.model_dump(serialize_as_any=True)\n","handlingStrategy":"validation","validationCode":"import pydantic\nif pydantic.VERSION.startswith('1.'):\n    kwargs.pop('serialize_as_any', None)\nobj.model_dump(**kwargs)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Design polymorphic fields with explicit subclass types or a discriminated union to avoid needing serialize_as_any"],"tags":["pydantic","serialization","v1-compat","polymorphism"],"backgroundTag":"pydantic-version-mismatch","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}