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
{{ message }}
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
After digging into this for a while it seems like we have a bit of a problem.
tl;dr: We cannot have working validation for DocList and have working jsonschema at the same time. The reason our validation currently works at all is that we have a bug that breaks pydantic in just the right way.
longer version:
I discovered that DocList does not fully behave like a built-in generic: get_origin(DocList[Doc]) returns None instead of DocList. This means that pydantic does not pick it up as a list and does not treat it properly. For most use cases this can be worked around though, by addiing __origin__ and __args__ like below, and pydantic's custom implementation of get_origin will pick it up properly:
The step above fixes Jsonschema and FastAPI, great success!
However, then validation does not work anymore:
classMyDoc(BaseDoc):
docs: DocList[ImageDoc]
d=MyDoc(docs=DocList[ImageDoc]([ImageDoc()]))
print(type(d.docs)) # this will be a `list` instead of `DocList[ImageDoc]`
This error can be reproduced in "pure" pydantic, showing that it is not per se a docarray issue:
T_item=TypeVar('T_item')
classMyOtherList(List[T_item], typing.Generic[T_item]):
@classmethoddef__get_validators__(cls):
yieldcls.validate@classmethoddefvalidate(cls, value, field, config):
print('validating :)') # this is never called/printedifisinstance(value, MyList):
print(f'{type(value)=}')
returnvalueraiseTypeError(f'Cannot validate')
classOtherDoc(BaseModel):
docs: MyOtherList[int]
d=OtherDoc(docs=MyOtherList[int]([1, 2, 3]))
print(type(d.docs)) # this will be a list instead of MyList
Basically, as soon as we actually make DocList a proper python list, validation doesn't work anymore :/
Conclusion: The reason validation on DocList works right now is that we failed to implement some list-like things, and so pydantic doesn't pick it up as a real list. If we fix this bug, validation will break. Thus, we cannot have working validation and working jsonschema/fastapi at the same time.
Activity
JohannesMessner commentedon May 16, 2023
After digging into this for a while it seems like we have a bit of a problem.
tl;dr: We cannot have working validation for
DocListand have working jsonschema at the same time. The reason our validation currently works at all is that we have a bug that breaks pydantic in just the right way.longer version:
DocListdoes not fully behave like a built-in generic:get_origin(DocList[Doc])returnsNoneinstead ofDocList. This means that pydantic does not pick it up as a list and does not treat it properly. For most use cases this can be worked around though, by addiing__origin__and__args__like below, and pydantic's custom implementation ofget_originwill pick it up properly:This error can be reproduced in "pure" pydantic, showing that it is not per se a docarray issue:
DocLista proper pythonlist, validation doesn't work anymore :/Conclusion: The reason validation on
DocListworks right now is that we failed to implement some list-like things, and so pydantic doesn't pick it up as a real list. If we fix this bug, validation will break. Thus, we cannot have working validation and working jsonschema/fastapi at the same time.samsja commentedon May 16, 2023
So we wait for Pydantic v2 to fix this ? Or we can undo the list like things and try to fix it (I don't think we should do it )
JohannesMessner commentedon May 16, 2023
I would wait
JoanFM commentedon May 16, 2023
We wait and document