You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an use case where I need to load data from the database over REST API (FastAPI) and the user can choose which fields to include/exclude to the data to gain performance improvements by leaving out some non-interesting data (leads significant improvements sometimes). I'm currently marking the non-loaded fields by setting value None, but this is problematic since for optional fields None is a valid value, thus it's not clear from the return data whether the field was loaded or if the real value is None. Instead of None, I would like to use Ellipsis (or ...) as a value for non-loaded fields, since it's another available standard Python type I could use instead of any custom classes. Maybe not the most typical use case, but it would make perfect sense for us.
So, my response model would look something like following:
class ModelResponse(BaseModel):
field_1: int | None | EllipsisType # Optional, use ellipsis if not loaded
field_2: uuid.UUID | EllipsisType # Required, use ellipsis if not loaded
# I would like to type Ellipsis as int | None | ... (like it's done for None vs. NoneType), but not possible in Python
# model_config = ConfigDict(
# arbitrary_types_allowed=True,
# json_encoders={
# EllipsisType: lambda v: "...",
# },
# )
However, this will lead to an error: pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'ellipsis'>. Set arbitrary_types_allowed=Truein the model_config to ignore this error or implementget_pydantic_core_schema on your type to fully support it.
Obviously, I can't implement __get_pydantic_core_schema__ for Ellipsis, thus my only option would be to use arbitrary_types_allowed=True ( which I don't want to do). If I use arbitrary_types_allowed=True, then everything works if using model_dump(mode="python") but fails with model_dump(mode="json"). pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'ellipsis'>
This one I could solve using json_encoders for EllipsisType and encode it to string or dict for example, but then my problem is how to set decoding such that all strings/dicts using the ellipsis format would be decoded back to Ellipsis in ModelResponse .model_validate(data). I couldn't find anything like json_decoders from Pydantic v2.
Any suggestions how I could make this everything work with Ellipsis in Pydantic v2? Since Ellipsis is a standard Python object/type, I would expect Pydantic to support it if it's typed correctly for the field. Then it's another topic how it should be serialised to JSON. For our use case, even "..." would work perfectly fine, for generic solution it should be probably something else. Anyway, I feel like there should be some easy way to implement this without need to use custom types for fields instead of standard Ellipsis.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have an use case where I need to load data from the database over REST API (FastAPI) and the user can choose which fields to include/exclude to the data to gain performance improvements by leaving out some non-interesting data (leads significant improvements sometimes). I'm currently marking the non-loaded fields by setting value
None
, but this is problematic since for optional fieldsNone
is a valid value, thus it's not clear from the return data whether the field was loaded or if the real value isNone
. Instead ofNone
, I would like to useEllipsis
(or...
) as a value for non-loaded fields, since it's another available standard Python type I could use instead of any custom classes. Maybe not the most typical use case, but it would make perfect sense for us.So, my response model would look something like following:
However, this will lead to an error:
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'ellipsis'>. Set
arbitrary_types_allowed=Truein the model_config to ignore this error or implement
get_pydantic_core_schemaon your type to fully support it.
Obviously, I can't implement
__get_pydantic_core_schema__
forEllipsis
, thus my only option would be to usearbitrary_types_allowed=True
( which I don't want to do). If I usearbitrary_types_allowed=True
, then everything works if usingmodel_dump(mode="python")
but fails withmodel_dump(mode="json")
.pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'ellipsis'>
This one I could solve using
json_encoders
forEllipsisType
and encode it to string or dict for example, but then my problem is how to set decoding such that all strings/dicts using the ellipsis format would be decoded back to Ellipsis inModelResponse .model_validate(data)
. I couldn't find anything likejson_decoders
from Pydantic v2.Any suggestions how I could make this everything work with
Ellipsis
in Pydantic v2? SinceEllipsis
is a standard Python object/type, I would expect Pydantic to support it if it's typed correctly for the field. Then it's another topic how it should be serialised to JSON. For our use case, even"..."
would work perfectly fine, for generic solution it should be probably something else. Anyway, I feel like there should be some easy way to implement this without need to use custom types for fields instead of standardEllipsis
.Beta Was this translation helpful? Give feedback.
All reactions