Skip to content

Implement a provider / provider_name version of serializable models#176

Closed
anbuzin wants to merge 2 commits into
mainfrom
make-models-serializable-2
Closed

Implement a provider / provider_name version of serializable models#176
anbuzin wants to merge 2 commits into
mainfrom
make-models-serializable-2

Conversation

@anbuzin

@anbuzin anbuzin commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ai-python Ready Ready Preview, Comment Jun 17, 2026 11:13pm

@msullivan msullivan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some comments about the interface but I still think this is the way forward

Comment thread examples/.test_scripts/run-with-patched-model.py Outdated
Comment thread examples/openai_chat_completions.py Outdated
Comment thread src/ai/models/core/model.py Outdated
Comment thread src/ai/models/core/model.py Outdated
Comment thread src/ai/models/core/model.py
Comment on lines +206 to +282
def model_dump(
self,
*,
mode: Literal["json", "python"] | str = "python",
include: Any = None,
exclude: Any = None,
context: Any | None = None,
by_alias: bool | None = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal["none", "warn", "error"] = True,
fallback: Callable[[Any], Any] | None = None,
serialize_as_any: bool = False,
) -> dict[str, Any]:
if mode == "json" and not self._is_serializable:
raise ConfigurationError(
"Model was constructed with a live provider/protocol and "
"cannot be serialized. Use provider_name/provider_args instead."
)
return super().model_dump(
mode=mode,
include=include,
exclude=exclude,
context=context,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
serialize_as_any=serialize_as_any,
)

def model_dump_json(
self,
*,
indent: int | None = None,
ensure_ascii: bool = False,
include: Any = None,
exclude: Any = None,
context: Any | None = None,
by_alias: bool | None = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal["none", "warn", "error"] = True,
fallback: Callable[[Any], Any] | None = None,
serialize_as_any: bool = False,
) -> str:
if not self._is_serializable:
raise ConfigurationError(
"Model was constructed with a live provider/protocol and "
"cannot be serialized. Use provider_name/provider_args instead."
)
return super().model_dump_json(
indent=indent,
ensure_ascii=ensure_ascii,
include=include,
exclude=exclude,
context=context,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
exclude_computed_fields=exclude_computed_fields,
round_trip=round_trip,
warnings=warnings,
fallback=fallback,
serialize_as_any=serialize_as_any,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Shouldn't _serialize_model handle this?

handler: pydantic.SerializerFunctionWrapHandler,
info: pydantic.SerializationInfo,
) -> dict[str, Any]:
if info.mode == "json" and not self._is_serializable:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, why only json mode?

@anbuzin anbuzin closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants