Extras API¶
This page documents the extra adapters provided by pydapter.
Excel Adapter¶
pydapter.extras.excel_
¶
Excel adapter (requires pandas + xlsxwriter engine).
Classes¶
ExcelAdapter
¶
Bases: Adapter[T]
Adapter for converting between Pydantic models and Excel files.
This adapter handles Excel (.xlsx) files, providing methods to: - Read Excel files into Pydantic model instances - Write Pydantic models to Excel files - Support for different sheets and pandas read_excel options
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("xlsx") |
Example
from pathlib import Path
from pydantic import BaseModel
from pydapter.extras.excel_ import ExcelAdapter
class Person(BaseModel):
name: str
age: int
# Read from Excel file
excel_file = Path("people.xlsx")
people = ExcelAdapter.from_obj(Person, excel_file, many=True)
# Write to Excel file
output_bytes = ExcelAdapter.to_obj(people, many=True)
with open("output.xlsx", "wb") as f:
f.write(output_bytes)
Source code in src/pydapter/extras/excel_.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
Functions¶
from_obj(subj_cls, obj, /, *, many=True, sheet_name=0, **kw)
classmethod
¶
Convert Excel data to Pydantic model instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subj_cls
|
type[T]
|
The Pydantic model class to instantiate |
required |
obj
|
str | Path | bytes
|
Excel file path, file-like object, or bytes |
required |
many
|
bool
|
If True, convert all rows; if False, convert only first row |
True
|
sheet_name
|
str | int
|
Sheet name or index to read (default: 0) |
0
|
**kw
|
Any
|
Additional arguments passed to pandas.read_excel |
{}
|
Returns:
Type | Description |
---|---|
T | list[T]
|
List of model instances if many=True, single instance if many=False |
Raises:
Type | Description |
---|---|
ResourceError
|
If the Excel file cannot be read |
AdapterError
|
If the data cannot be converted to models |
Source code in src/pydapter/extras/excel_.py
Pandas Adapter¶
pydapter.extras.pandas_
¶
DataFrame & Series adapters (require pandas
).
Classes¶
DataFrameAdapter
¶
Bases: Adapter[T]
Adapter for converting between Pydantic models and pandas DataFrames.
This adapter handles pandas DataFrame objects, providing methods to: - Convert DataFrame rows to Pydantic model instances - Convert Pydantic models to DataFrame rows - Handle both single records and multiple records
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("pd.DataFrame") |
Example
import pandas as pd
from pydantic import BaseModel
from pydapter.extras.pandas_ import DataFrameAdapter
class Person(BaseModel):
name: str
age: int
# Create DataFrame
df = pd.DataFrame([
{"name": "John", "age": 30},
{"name": "Jane", "age": 25}
])
# Convert to Pydantic models
people = DataFrameAdapter.from_obj(Person, df, many=True)
# Convert back to DataFrame
df_output = DataFrameAdapter.to_obj(people, many=True)
Source code in src/pydapter/extras/pandas_.py
Functions¶
from_obj(subj_cls, obj, /, *, many=True, **kw)
classmethod
¶
Convert DataFrame to Pydantic model instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subj_cls
|
type[T]
|
The Pydantic model class to instantiate |
required |
obj
|
DataFrame
|
The pandas DataFrame to convert |
required |
many
|
bool
|
If True, convert all rows; if False, convert only first row |
True
|
**kw
|
Any
|
Additional arguments passed to model_validate |
{}
|
Returns:
Type | Description |
---|---|
T | list[T]
|
List of model instances if many=True, single instance if many=False |
Source code in src/pydapter/extras/pandas_.py
to_obj(subj, /, *, many=True, **kw)
classmethod
¶
Convert Pydantic model instances to pandas DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subj
|
T | list[T]
|
Single model instance or list of instances |
required |
many
|
bool
|
If True, handle as multiple instances |
True
|
**kw
|
Any
|
Additional arguments passed to DataFrame constructor |
{}
|
Returns:
Type | Description |
---|---|
DataFrame
|
pandas DataFrame with model data |
Source code in src/pydapter/extras/pandas_.py
SeriesAdapter
¶
Bases: Adapter[T]
Adapter for converting between Pydantic models and pandas Series.
This adapter handles pandas Series objects, providing methods to: - Convert Series to a single Pydantic model instance - Convert Pydantic model to Series - Only supports single records (many=False)
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("pd.Series") |
Example
import pandas as pd
from pydantic import BaseModel
from pydapter.extras.pandas_ import SeriesAdapter
class Person(BaseModel):
name: str
age: int
# Create Series
series = pd.Series({"name": "John", "age": 30})
# Convert to Pydantic model
person = SeriesAdapter.from_obj(Person, series)
# Convert back to Series
series_output = SeriesAdapter.to_obj(person)
Source code in src/pydapter/extras/pandas_.py
Functions¶
from_obj(subj_cls, obj, /, *, many=False, **kw)
classmethod
¶
Convert pandas Series to Pydantic model instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subj_cls
|
type[T]
|
The Pydantic model class to instantiate |
required |
obj
|
Series
|
The pandas Series to convert |
required |
many
|
bool
|
Must be False (Series only supports single records) |
False
|
**kw
|
Any
|
Additional arguments passed to model_validate |
{}
|
Returns:
Type | Description |
---|---|
T
|
Single model instance |
Raises:
Type | Description |
---|---|
ValueError
|
If many=True is specified |
Source code in src/pydapter/extras/pandas_.py
SQL Adapter¶
pydapter.extras.sql_
¶
Generic SQL adapter using SQLAlchemy Core (requires sqlalchemy>=2.0
).
Classes¶
SQLAdapter
¶
Bases: Adapter[T]
Generic SQL adapter using SQLAlchemy Core for database operations.
This adapter provides methods to: - Execute SQL queries and convert results to Pydantic models - Insert Pydantic models as rows into database tables - Support for various SQL databases through SQLAlchemy - Handle both raw SQL and table-based operations
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("sql") |
Example
import sqlalchemy as sa
from pydantic import BaseModel
from pydapter.extras.sql_ import SQLAdapter
class User(BaseModel):
id: int
name: str
email: str
# Setup database connection
engine = sa.create_engine("sqlite:///example.db")
metadata = sa.MetaData()
# Query from database
query = "SELECT id, name, email FROM users WHERE active = true"
users = SQLAdapter.from_obj(
User,
query,
many=True,
engine=engine
)
# Insert to database
new_users = [User(id=1, name="John", email="john@example.com")]
SQLAdapter.to_obj(
new_users,
many=True,
table="users",
metadata=metadata
)
Source code in src/pydapter/extras/sql_.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
PostgreSQL Adapter¶
pydapter.extras.postgres_
¶
PostgresAdapter - thin preset over SQLAdapter (pgvector-ready if you add vec column).
Classes¶
PostgresAdapter
¶
Bases: SQLAdapter[T]
PostgreSQL-specific adapter extending SQLAdapter with PostgreSQL optimizations.
This adapter provides: - PostgreSQL-specific connection handling and error messages - Default PostgreSQL connection string - Enhanced error handling for common PostgreSQL issues - Support for pgvector when vector columns are present
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("postgres") |
|
DEFAULT |
Default PostgreSQL connection string |
Example
from pydantic import BaseModel
from pydapter.extras.postgres_ import PostgresAdapter
class User(BaseModel):
id: int
name: str
email: str
# Query with custom connection
query_config = {
"query": "SELECT id, name, email FROM users WHERE active = true",
"engine_url": "postgresql+psycopg://user:pass@localhost/mydb"
}
users = PostgresAdapter.from_obj(User, query_config, many=True)
# Insert with default connection
insert_config = {
"table": "users",
"engine_url": "postgresql+psycopg://user:pass@localhost/mydb"
}
new_users = [User(id=1, name="John", email="john@example.com")]
PostgresAdapter.to_obj(new_users, insert_config, many=True)
Source code in src/pydapter/extras/postgres_.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
MongoDB Adapter¶
pydapter.extras.mongo_
¶
MongoDB adapter (requires pymongo
).
Classes¶
MongoAdapter
¶
Bases: Adapter[T]
MongoDB adapter for converting between Pydantic models and MongoDB documents.
This adapter provides methods to: - Query MongoDB collections and convert documents to Pydantic models - Insert Pydantic models as documents into MongoDB collections - Handle MongoDB connection management and error handling - Support for various MongoDB operations (find, insert, update, delete)
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("mongo") |
Example
from pydantic import BaseModel
from pydapter.extras.mongo_ import MongoAdapter
class User(BaseModel):
name: str
email: str
age: int
# Query from MongoDB
query_config = {
"url": "mongodb://localhost:27017",
"database": "myapp",
"collection": "users",
"filter": {"age": {"$gte": 18}}
}
users = MongoAdapter.from_obj(User, query_config, many=True)
# Insert to MongoDB
insert_config = {
"url": "mongodb://localhost:27017",
"database": "myapp",
"collection": "users"
}
new_users = [User(name="John", email="john@example.com", age=30)]
MongoAdapter.to_obj(new_users, insert_config, many=True)
Source code in src/pydapter/extras/mongo_.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
Neo4j Adapter¶
pydapter.extras.neo4j_
¶
Neo4j adapter (requires neo4j
).
Classes¶
Neo4jAdapter
¶
Bases: Adapter[T]
Neo4j graph database adapter for converting between Pydantic models and Neo4j nodes/relationships.
This adapter provides methods to: - Execute Cypher queries and convert results to Pydantic models - Create nodes and relationships from Pydantic models - Handle Neo4j connection management and error handling - Support for complex graph operations and traversals
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("neo4j") |
Example
from pydantic import BaseModel
from pydapter.extras.neo4j_ import Neo4jAdapter
from neo4j import basic_auth
class Person(BaseModel):
name: str
age: int
city: str
# Query from Neo4j
query_config = {
"url": "bolt://localhost:7687",
"auth": basic_auth("neo4j", "password"),
"query": "MATCH (p:Person) WHERE p.age >= 18 RETURN p.name, p.age, p.city"
}
people = Neo4jAdapter.from_obj(Person, query_config, many=True)
# Create nodes in Neo4j
create_config = {
"url": "bolt://localhost:7687",
"auth": basic_auth("neo4j", "password"),
"query": "CREATE (p:Person {name: $name, age: $age, city: $city})"
}
new_people = [Person(name="John", age=30, city="NYC")]
Neo4jAdapter.to_obj(new_people, create_config, many=True)
Source code in src/pydapter/extras/neo4j_.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
Qdrant Adapter¶
pydapter.extras.qdrant_
¶
Qdrant vector-store adapter (requires qdrant-client
).
Classes¶
QdrantAdapter
¶
Bases: Adapter[T]
Qdrant vector database adapter for converting between Pydantic models and vector embeddings.
This adapter provides methods to: - Search for similar vectors and convert results to Pydantic models - Insert Pydantic models as vector points into Qdrant collections - Handle vector similarity operations and metadata filtering - Support for both cloud and self-hosted Qdrant instances
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("qdrant") |
Example
from pydantic import BaseModel
from pydapter.extras.qdrant_ import QdrantAdapter
class Document(BaseModel):
id: str
text: str
embedding: list[float]
category: str
# Search for similar vectors
search_config = {
"url": "http://localhost:6333",
"collection_name": "documents",
"query_vector": [0.1, 0.2, 0.3, ...], # 768-dim vector
"limit": 10,
"score_threshold": 0.8
}
similar_docs = QdrantAdapter.from_obj(Document, search_config, many=True)
# Insert documents with vectors
insert_config = {
"url": "http://localhost:6333",
"collection_name": "documents"
}
new_docs = [Document(
id="doc1",
text="Sample text",
embedding=[0.1, 0.2, 0.3, ...],
category="tech"
)]
QdrantAdapter.to_obj(new_docs, insert_config, many=True)
Source code in src/pydapter/extras/qdrant_.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
Async SQL Adapter¶
pydapter.extras.async_sql_
¶
Generic async SQL adapter - SQLAlchemy 2.x asyncio + asyncpg driver.
Classes¶
AsyncSQLAdapter
¶
Bases: AsyncAdapter[T]
Asynchronous SQL adapter using SQLAlchemy 2.x asyncio for database operations.
This adapter provides async methods to: - Execute SQL queries asynchronously and convert results to Pydantic models - Insert Pydantic models as rows into database tables asynchronously - Support for various async SQL databases through SQLAlchemy - Handle connection pooling and async context management
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("async_sql") |
Example
import asyncio
import sqlalchemy as sa
from pydantic import BaseModel
from pydapter.extras.async_sql_ import AsyncSQLAdapter
class User(BaseModel):
id: int
name: str
email: str
async def main():
# Query from database
query_config = {
"engine_url": "postgresql+asyncpg://user:pass@localhost/db",
"query": "SELECT id, name, email FROM users WHERE active = true"
}
users = await AsyncSQLAdapter.from_obj(User, query_config, many=True)
# Insert to database
insert_config = {
"engine_url": "postgresql+asyncpg://user:pass@localhost/db",
"table": "users"
}
new_users = [User(id=1, name="John", email="john@example.com")]
await AsyncSQLAdapter.to_obj(new_users, insert_config, many=True)
asyncio.run(main())
Source code in src/pydapter/extras/async_sql_.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
Async PostgreSQL Adapter¶
pydapter.extras.async_postgres_
¶
AsyncPostgresAdapter - presets AsyncSQLAdapter for PostgreSQL/pgvector.
Classes¶
AsyncPostgresAdapter
¶
Bases: AsyncSQLAdapter[T]
Asynchronous PostgreSQL adapter extending AsyncSQLAdapter with PostgreSQL-specific optimizations.
This adapter provides: - Async PostgreSQL operations using asyncpg driver - Enhanced error handling for PostgreSQL-specific issues - Support for pgvector when vector columns are present - Default PostgreSQL connection string management
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("async_pg") |
|
DEFAULT |
Default PostgreSQL+asyncpg connection string |
Example
import asyncio
from pydantic import BaseModel
from pydapter.extras.async_postgres_ import AsyncPostgresAdapter
class User(BaseModel):
id: int
name: str
email: str
async def main():
# Query with custom connection
query_config = {
"query": "SELECT id, name, email FROM users WHERE active = true",
"dsn": "postgresql+asyncpg://user:pass@localhost/mydb"
}
users = await AsyncPostgresAdapter.from_obj(User, query_config, many=True)
# Insert with default connection
insert_config = {
"table": "users"
}
new_users = [User(id=1, name="John", email="john@example.com")]
await AsyncPostgresAdapter.to_obj(new_users, insert_config, many=True)
asyncio.run(main())
Source code in src/pydapter/extras/async_postgres_.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
Async MongoDB Adapter¶
pydapter.extras.async_mongo_
¶
AsyncMongoAdapter - uses motor.motor_asyncio
.
Classes¶
AsyncMongoAdapter
¶
Bases: AsyncAdapter[T]
Asynchronous MongoDB adapter for converting between Pydantic models and MongoDB documents.
This adapter provides async methods to: - Query MongoDB collections asynchronously and convert documents to Pydantic models - Insert Pydantic models as documents into MongoDB collections asynchronously - Handle async MongoDB operations using Motor (async MongoDB driver) - Support for various async MongoDB operations (find, insert, update, delete)
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("async_mongo") |
Example
import asyncio
from pydantic import BaseModel
from pydapter.extras.async_mongo_ import AsyncMongoAdapter
class User(BaseModel):
name: str
email: str
age: int
async def main():
# Query from MongoDB
query_config = {
"url": "mongodb://localhost:27017",
"database": "myapp",
"collection": "users",
"filter": {"age": {"$gte": 18}}
}
users = await AsyncMongoAdapter.from_obj(User, query_config, many=True)
# Insert to MongoDB
insert_config = {
"url": "mongodb://localhost:27017",
"database": "myapp",
"collection": "users"
}
new_users = [User(name="John", email="john@example.com", age=30)]
await AsyncMongoAdapter.to_obj(new_users, insert_config, many=True)
asyncio.run(main())
Source code in src/pydapter/extras/async_mongo_.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
Async Qdrant Adapter¶
pydapter.extras.async_qdrant_
¶
AsyncQdrantAdapter - vector upsert / search using AsyncQdrantClient.
Classes¶
AsyncQdrantAdapter
¶
Bases: AsyncAdapter[T]
Asynchronous Qdrant vector database adapter for async vector operations.
This adapter provides async methods to: - Search for similar vectors asynchronously and convert results to Pydantic models - Insert Pydantic models as vector points into Qdrant collections asynchronously - Handle async vector similarity operations and metadata filtering - Support for both cloud and self-hosted Qdrant instances with async operations
Attributes:
Name | Type | Description |
---|---|---|
obj_key |
The key identifier for this adapter type ("async_qdrant") |
Example
import asyncio
from pydantic import BaseModel
from pydapter.extras.async_qdrant_ import AsyncQdrantAdapter
class Document(BaseModel):
id: str
text: str
embedding: list[float]
category: str
async def main():
# Search for similar vectors
search_config = {
"url": "http://localhost:6333",
"collection_name": "documents",
"query_vector": [0.1, 0.2, 0.3, ...], # 768-dim vector
"limit": 10,
"score_threshold": 0.8
}
similar_docs = await AsyncQdrantAdapter.from_obj(Document, search_config, many=True)
# Insert documents with vectors
insert_config = {
"url": "http://localhost:6333",
"collection_name": "documents"
}
new_docs = [Document(
id="doc1",
text="Sample text",
embedding=[0.1, 0.2, 0.3, ...],
category="tech"
)]
await AsyncQdrantAdapter.to_obj(new_docs, insert_config, many=True)
asyncio.run(main())
Source code in src/pydapter/extras/async_qdrant_.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|