support binding classes on Pydantic Models to CocoIndex Struct #1072
+296
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Now cocoindex supports the binding classes on pydantic model to cocoindex struct as well as updated docs according to the new feature
Closes Issue: #795
Tests passed

Also written file (not pushed) to test pydantic models working (results are below along with code)
Code:
Output:
=== CocoIndex Pydantic Support Demo === 1. Type Detection: is_struct_type(PersonDataclass): True is_struct_type(PersonNamedTuple): True is_struct_type(PersonPydantic): True is_pydantic_model(PersonPydantic): True 2. Schema Encoding: Dataclass schema: ({'fields': [{'type': {'kind': 'Str'}, 'name': 'name'}, {'type': {'kind': 'Int64'}, 'name': 'age'}, {'type': {'kind': 'Float64'}, 'name': 'score'}], 'description': 'PersonDataclass(name: str, age: int, score: float)'}, None) NamedTuple schema: ({'fields': [{'type': {'kind': 'Str'}, 'name': 'name'}, {'type': {'kind': 'Int64'}, 'name': 'age'}, {'type': {'kind': 'Float64'}, 'name': 'score'}], 'description': 'PersonNamedTuple(name, age, score)'}, None) Pydantic schema: ({'fields': [{'type': {'kind': 'Str'}, 'name': 'name'}, {'type': {'kind': 'Int64'}, 'name': 'age'}, {'type': {'kind': 'Float64'}, 'name': 'score'}], 'description': '!!! abstract "Usage Documentation"\n [Models](../concepts/models.md)\n\nA base class for creating Pydantic models.\n\nAttributes:\n __class_vars__: The names of the class variables defined on the model.\n __private_attributes__: Metadata about the private attributes of the model.\n __signature__: The synthesized `__init__` [`Signature`][inspect.Signature] of the model.\n\n __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.\n __pydantic_core_schema__: The core schema of the model.\n __pydantic_custom_init__: Whether the model has a custom `__init__` function.\n __pydantic_decorators__: Metadata containing the decorators defined on the model.\n This replaces `Model.__validators__` and `Model.__root_validators__` from Pydantic V1.\n __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to\n __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.\n __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.\n __pydantic_post_init__: The name of the post-init method for the model, if defined.\n __pydantic_root_model__: Whether the model is a [`RootModel`][pydantic.root_model.RootModel].\n __pydantic_serializer__: The `pydantic-core` `SchemaSerializer` used to dump instances of the model.\n __pydantic_validator__: The `pydantic-core` `SchemaValidator` used to validate instances of the model.\n\n __pydantic_fields__: A dictionary of field names and their corresponding [`FieldInfo`][pydantic.fields.FieldInfo] objects.\n __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [`ComputedFieldInfo`][pydantic.fields.ComputedFieldInfo] objects.\n\n __pydantic_extra__: A dictionary containing extra values, if [`extra`][pydantic.config.ConfigDict.extra]\n is set to `\'allow\'`.\n __pydantic_fields_set__: The names of fields explicitly set during instantiation.\n __pydantic_private__: Values of private attributes set on the model instance.'}, None) All schemas equal: False 3. Value Encoding: Dataclass encoded: ['Alice', 30, 95.5] NamedTuple encoded: ['Alice', 30, 95.5] Pydantic encoded: ['Alice', 30, 95.5] All encodings equal: True 4. Encoded Values Verification: Expected: ['Alice', 30, 95.5] All match expected: True 5. Pydantic Model Features: person_py.name: Alice person_py.age: 30 person_py.score: 95.5 person_py.model_dump(): {'name': 'Alice', 'age': 30, 'score': 95.5} All Pydantic support tests passed! Summary: - Pydantic models are detected as struct types - Schema encoding works correctly - Value encoding produces expected results - Pydantic models work alongside dataclasses and NamedTuples 6. Pydantic-Specific Features: Pydantic validation caught error: ValidationError: 1 validation error for PersonPydantic age Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='not_a_number', input_type=str] For further information visit https://errors.pydantic.dev/2.11/v/int_parsing Pydantic model_dump(): {'name': 'Alice', 'age': 30, 'score': 95.5} Pydantic from dict: name='Alice' age=30 score=95.5 === Demo Complete ===