[FEATURE] Support union type for basic types #436
Labels
cocoindex-core
CocoIndex Core Engine
help wanted
Extra attention is needed
python-sdk
CocoIndex Python SDK
Uh oh!
There was an error while loading. Please reload this page.
We want to support union type for basic types.
Background
Union types are supported in Python (e.g.
str | int
). CocoIndex doesn't support it now, so when a library already defines a dataclass with Union type members, users cannot use the existing type as argument / return type of a CocoIndex function (existing example from relik).Alternative solution considered
Bind union type in Python to Json type in CocoIndex
However, Json type is too generic and loses essential type information. Unexpected values can be set to the field without being noticed earlier (e.g. when calling LLM to create the dataclass value).
With this said, binding an unsupported type to Json still can be supported, because there're still types that cannot be supported even after we support union type for basic types. This will be treated as a separate feature request.
Not limit union type to basic type
i.e. we may support union type for struct/table types too.
We may support it eventually, but it's difficult to reliably interpret the type for a specific struct value, especially our struct types are unnamed (for now).
Things to Change
We want to add
Union
as a new variant forBasicValueType
, e.g.We considered putting it as a new type at the same level as
BasicValueType
,StructType
andTableType
. One practical reason of not doing so is: things likelist[str | int]
will not be representable. ConsideringVector
is already part ofBasicValueType
, havingUnion
as anotherBasicValueType
makes sense.Other code to update:
BasicValueType
, for serializing, deserializing, etc.BasicValue::from_json()
. It needs to updated.JsonSchemaBuilder
typing.py
, maybe alsoconvert.py
.Please also the test
test_convert.py
data_types.mdx
❤️ Contributors, please refer to 📙Contributing Guide.
Unless the PR can be sent immediately (e.g. just a few lines of code), we recommend you to leave a comment on the issue like
I'm working on it
orCan I work on this issue?
to avoid duplicating work. Our Discord server is always open and friendly.The text was updated successfully, but these errors were encountered: