Skip to content

Commit 5a550dc

Browse files
authored
chore(pydantic): add optional dependency for testing, docs minor cleanup (#1073)
* chore(pydantic): add optional dependency for testing, docs minor cleanup * docs: more comments * chore: add `pydantic` as an optional dependency
1 parent d309076 commit 5a550dc

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

docs/docs/core/data_types.mdx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ These options define a structured type with named fields, but they differ slight
113113
- **Dataclass**: A flexible class-based structure, mutable by default, defined using the `@dataclass` decorator.
114114
- **NamedTuple**: An immutable tuple-based structure, defined using `typing.NamedTuple`.
115115
- **Pydantic model**: A modern data validation and parsing structure, defined by inheriting from `pydantic.BaseModel`.
116+
Make sure you installed the `pydantic` package when using Pydantic model.
116117

117118
For example:
118119

119120
```python
120121
from dataclasses import dataclass
121122
from typing import NamedTuple
123+
from pydantic import BaseModel # requires `pydantic` package to be installed
122124
import datetime
123125

124126
# Using dataclass
@@ -134,17 +136,11 @@ class PersonTuple(NamedTuple):
134136
last_name: str
135137
dob: datetime.date
136138

137-
# Using Pydantic (optional dependency)
138-
try:
139-
from pydantic import BaseModel
140-
141-
class PersonModel(BaseModel):
142-
first_name: str
143-
last_name: str
144-
dob: datetime.date
145-
except ImportError:
146-
# Pydantic is optional
147-
pass
139+
# Using Pydantic
140+
class PersonModel(BaseModel):
141+
first_name: str
142+
last_name: str
143+
dob: datetime.date
148144
```
149145

150146
All three examples (`Person`, `PersonTuple`, and `PersonModel`) are valid Struct types in CocoIndex, with identical schemas (three fields: `first_name` (Str), `last_name` (Str), `dob` (Date)).

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ dev = ["pytest", "pytest-asyncio", "ruff", "mypy", "pre-commit"]
6767
embeddings = ["sentence-transformers>=3.3.1"]
6868
colpali = ["colpali-engine"]
6969
lancedb = ["lancedb>=0.25.0"]
70+
pydantic = ["pydantic>=2.11.9"]
7071

7172
# We need to repeat the dependency above to make it available for the `all` feature.
7273
# Indirect dependencies such as "cocoindex[embeddings]" will not work for local development.
73-
all = ["sentence-transformers>=3.3.1", "colpali-engine", "lancedb>=0.25.0"]
74+
all = [
75+
"sentence-transformers>=3.3.1",
76+
"colpali-engine",
77+
"lancedb>=0.25.0",
78+
"pydantic>=2.11.9",
79+
]
7480

7581
[tool.mypy]
7682
python_version = "3.11"

0 commit comments

Comments
 (0)