Skip to content

[FEATURE] Support union type for basic types #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
badmonster0 opened this issue May 6, 2025 · 1 comment
Open

[FEATURE] Support union type for basic types #436

badmonster0 opened this issue May 6, 2025 · 1 comment
Assignees
Labels
cocoindex-core CocoIndex Core Engine help wanted Extra attention is needed python-sdk CocoIndex Python SDK

Comments

@badmonster0
Copy link
Member

badmonster0 commented May 6, 2025

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 for BasicValueType, e.g.

Union(Vec<BasicValueType>),

We considered putting it as a new type at the same level as BasicValueType, StructType and TableType. One practical reason of not doing so is: things like list[str | int] will not be representable. Considering Vector is already part of BasicValueType, having Union as another BasicValueType makes sense.

Other code to update:


❤️ 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 or Can I work on this issue? to avoid duplicating work. Our Discord server is always open and friendly.

@badmonster0 badmonster0 added help wanted Extra attention is needed cocoindex-core CocoIndex Core Engine python-sdk CocoIndex Python SDK labels May 6, 2025
@chardoncs
Copy link

Hello, I'm working on it.

I have done some experiment in my fork, and it seems doable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cocoindex-core CocoIndex Core Engine help wanted Extra attention is needed python-sdk CocoIndex Python SDK
Projects
Status: 🚀 In Progress
Development

No branches or pull requests

2 participants