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
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 | |
Functions¶
from_obj(subj_cls, obj, /, *, many=True, adapt_meth='model_validate', sheet_name=0, adapt_kw=None, **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
|
adapt_meth
|
str
|
Method name to use for model validation (default: "model_validate") |
'model_validate'
|
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
to_obj(subj, /, *, many=True, adapt_meth='model_dump', adapt_kw=None, sheet_name='Sheet1', **kw)
classmethod
¶
Convert Pydantic model instances to Excel bytes.
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
|
adapt_meth
|
str
|
Method name to use for model dumping (default: "model_dump") |
'model_dump'
|
sheet_name
|
str
|
Name of the Excel sheet (default: "Sheet1") |
'Sheet1'
|
**kw
|
Any
|
Additional arguments passed to DataFrame constructor |
{}
|
Returns:
| Type | Description |
|---|---|
bytes
|
Excel file content as bytes |
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, adapt_meth='model_validate', adapt_kw=None, **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
|
adapt_meth
|
str
|
Method name to call on subj_cls (default: "model_validate") |
'model_validate'
|
**kw
|
Any
|
Additional arguments passed to the adaptation method |
{}
|
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, adapt_meth='model_dump', adapt_kw=None, **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
|
adapt_meth
|
str
|
Method name to call on model instances (default: "model_dump") |
'model_dump'
|
**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
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 | |
Functions¶
from_obj(subj_cls, obj, /, *, many=False, adapt_meth='model_validate', adapt_kw=None, **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
|
adapt_meth
|
str
|
Method name to call on subj_cls (default: "model_validate") |
'model_validate'
|
**kw
|
Any
|
Additional arguments passed to the adaptation method |
{}
|
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 | |
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 148 149 150 151 152 153 154 155 156 | |
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 260 261 262 263 264 265 | |
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 277 278 279 280 281 282 283 284 285 | |
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 343 344 345 346 347 | |
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 - Update, delete, and upsert operations through configuration - Execute raw SQL with parameterized queries - 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") |
Configuration Examples
from pydantic import BaseModel
from pydapter.extras.async_sql_ import AsyncSQLAdapter, SQLReadConfig, SQLWriteConfig
class User(BaseModel):
id: int
name: str
email: str
# Using TypedDict for type hints (recommended for IDE support)
config: SQLReadConfig = {
"dsn": "postgresql+asyncpg://user:pass@localhost/db",
"table": "users",
"selectors": {"active": True},
"limit": 10
}
users = await AsyncSQLAdapter.from_obj(User, config, many=True)
# Or inline dict (same as before)
users = await AsyncSQLAdapter.from_obj(User, {
"dsn": "postgresql+asyncpg://user:pass@localhost/db",
"table": "users",
"selectors": {"active": True},
"limit": 10
}, many=True)
# DELETE via config
result = await AsyncSQLAdapter.from_obj(User, {
"dsn": "postgresql+asyncpg://user:pass@localhost/db",
"operation": "delete",
"table": "users",
"selectors": {"id": 123}
})
# Raw SQL execution (note: table parameter NOT required)
result = await AsyncSQLAdapter.from_obj(User, {
"dsn": "postgresql+asyncpg://user:pass@localhost/db",
"operation": "raw_sql",
"sql": "SELECT * FROM users WHERE created_at > :since",
"params": {"since": "2024-01-01"}
}, many=True)
# Or with dict for flexible results (no model validation)
result = await AsyncSQLAdapter.from_obj(dict, {
"dsn": "postgresql+asyncpg://user:pass@localhost/db",
"operation": "raw_sql",
"sql": "SELECT * FROM users ORDER BY created_at DESC LIMIT :limit",
"params": {"limit": 10}
}, many=True)
# INSERT (default operation)
result = await AsyncSQLAdapter.to_obj(
new_user,
dsn="postgresql+asyncpg://user:pass@localhost/db",
table="users"
)
# UPDATE via config
result = await AsyncSQLAdapter.to_obj(
updated_user,
dsn="postgresql+asyncpg://user:pass@localhost/db",
table="users",
operation="update",
where={"id": 123}
)
# UPSERT via config
result = await AsyncSQLAdapter.to_obj(
user_data,
dsn="postgresql+asyncpg://user:pass@localhost/db",
table="users",
operation="upsert",
conflict_columns=["email"]
)
Source code in src/pydapter/extras/async_sql_.py
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
SQLReadConfig
¶
Bases: TypedDict
Configuration for SQL read operations (from_obj).
Source code in src/pydapter/extras/async_sql_.py
SQLWriteConfig
¶
Bases: TypedDict
Configuration for SQL write operations (to_obj as **kwargs).
Source code in src/pydapter/extras/async_sql_.py
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
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 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 | |
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 260 261 262 263 264 265 266 267 268 269 270 271 | |
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 308 309 310 311 312 313 314 315 316 317 318 | |