Loaders¶
Load external schemas or YAML specs into a canonical YadsSpec.
import pyarrow as pa
import yads
pyarrow_schema = pa.schema(
[
pa.field("submission_id", pa.int64(), nullable=False),
pa.field("completion_percent", pa.decimal128(5, 2)),
pa.field("time_taken_second", pa.int32()),
pa.field("submitted_at", pa.timestamp("ns", tz="UTC")),
]
)
spec = yads.from_pyarrow(
pyarrow_schema,
name="prod.assessments.submissions",
version=1,
)
print(spec)
spec prod.assessments.submissions(version=1)(
columns=[
submission_id: integer(bits=64)(
constraints=[NotNullConstraint()]
)
completion_percent: decimal(precision=5, scale=2, bits=128)
time_taken_second: integer(bits=32)
submitted_at: timestamptz(unit=ns, tz=UTC)
]
)
The following sections outline the high-level entry point functions available in
yads. You can refer to the dedicated loader documentation for their complete
API reference with more customization options.
| Source | Helper | Loader |
|---|---|---|
| YAML (path or stream) | yads.from_yaml |
yads.loaders.YamlLoader |
| PyArrow schema | yads.from_pyarrow |
yads.loaders.PyArrowLoader |
| PySpark StructType | yads.from_pyspark |
yads.loaders.PySparkLoader |
| Polars schema | yads.from_polars |
yads.loaders.PolarsLoader |
Other YAML helpers:
yads.from_yaml_string, yads.from_yaml_path, and yads.from_yaml_stream.
Shared customization options¶
PyArrow, PySpark, and Polars loaders expose the following arguments for shaping input into a valid spec:
modetoggles the load mode to one ofraiseorcoerce.fallback_typesets aStringorBinarydefault when coercing unsupported source types.
Loading mode¶
Switch between strict (mode="raise") and permissive (mode="coerce") runs per
call or via loader configuration. Here, a Dictionary column fails in raise mode.
unsupported_pyarrow_schema = pa.schema(
[
pa.field("submission_id", pa.int64(), nullable=False),
pa.field(
"attributes",
pa.dictionary(index_type=pa.int32(), value_type=pa.string()),
),
]
)
try:
yads.from_pyarrow(
unsupported_pyarrow_schema,
name="prod.assessments.submissions",
version=1,
mode="raise",
)
except Exception as exc:
print(type(exc).__name__ + ": " + str(exc))
UnsupportedFeatureError: PyArrowLoader does not support PyArrow type: 'dictionary<values=string, indices=int32, ordered=0>' (DictionaryType) for 'attributes'.
Fallback type¶
Provide a fallback YadsType to keep loading unsupported logical types when
running in coerce mode.
spec = yads.from_pyarrow(
unsupported_pyarrow_schema,
name="prod.assessments.submissions",
version=1,
mode="coerce",
fallback_type=ytypes.String(),
)
print(spec.columns[-1])
attributes: string
Wrapper helpers¶
from_yaml(source, *, encoding='utf-8')
¶
Load a spec from a path or a file-like stream.
This convenience loader avoids ambiguity by not accepting arbitrary content
strings. Pass content strings to from_yaml_string instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path | IO[str] | IO[bytes]
|
A filesystem path ( |
required |
encoding
|
str
|
Text encoding used when reading files or decoding binary streams. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Source code in src/yads/loaders/__init__.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
from_yaml_string(content)
¶
Load a spec from YAML string content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
YAML content as a string. |
required |
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Source code in src/yads/loaders/__init__.py
120 121 122 123 124 125 126 127 128 129 | |
from_yaml_path(path, *, encoding='utf-8')
¶
Load a spec from a YAML file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Filesystem path to a YAML file. |
required |
encoding
|
str
|
Text encoding used to read the file. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/yads/loaders/__init__.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
from_yaml_stream(stream, *, encoding='utf-8')
¶
Load a spec from a file-like stream.
The stream is not closed by this function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
IO[str] | IO[bytes]
|
File-like object opened in text or binary mode. |
required |
encoding
|
str
|
Used only if |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Source code in src/yads/loaders/__init__.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
from_dict(data)
¶
Load a YadsSpec from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The dictionary representation of the spec. |
required |
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
data = {
"name": "users",
"version": 1,
"columns": [
{
"name": "id",
"type": "integer",
},
{
"name": "email",
"type": "string",
}
]
}
spec = from_dict(data)
Source code in src/yads/loaders/__init__.py
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 | |
from_pyarrow(schema, *, mode='coerce', fallback_type=None, name, version, description=None)
¶
Load a spec from a pyarrow.Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
An instance of |
required |
mode
|
Literal['raise', 'coerce']
|
Loading mode. "raise" will raise exceptions on unsupported features. "coerce" will attempt to coerce unsupported features to supported ones with warnings. Defaults to "coerce". |
'coerce'
|
fallback_type
|
YadsType | None
|
A yads type to use as fallback when an unsupported PyArrow type is encountered. Only used when mode is "coerce". Must be either String or Binary, or None. Defaults to None. |
None
|
name
|
str
|
Fully-qualified spec name to assign. |
required |
version
|
int
|
Spec version string. |
required |
description
|
str | None
|
Optional human-readable description. |
None
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
import pyarrow as pa
schema = pa.schema([
pa.field("id", pa.int64()),
pa.field("name", pa.string()),
])
spec = from_pyarrow(schema, name="users", version=1)
Source code in src/yads/loaders/__init__.py
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 | |
from_pyspark(schema, *, mode='coerce', fallback_type=None, name, version, description=None)
¶
Load a spec from a pyspark.sql.types.StructType.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
An instance of |
required |
mode
|
Literal['raise', 'coerce']
|
Loading mode. "raise" will raise exceptions on unsupported features. "coerce" will attempt to coerce unsupported features to supported ones with warnings. Defaults to "coerce". |
'coerce'
|
fallback_type
|
YadsType | None
|
A yads type to use as fallback when an unsupported PySpark type is encountered. Only used when mode is "coerce". Must be either String or Binary, or None. Defaults to None. |
None
|
name
|
str
|
Fully-qualified spec name to assign. |
required |
version
|
int
|
Spec version string. |
required |
description
|
str | None
|
Optional human-readable description. |
None
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
from pyspark.sql.types import StructType, StructField, LongType, StringType
schema = StructType([
StructField("id", LongType(), nullable=False),
StructField("name", StringType(), nullable=True),
])
spec = from_pyspark(schema, name="users", version=1)
Source code in src/yads/loaders/__init__.py
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 | |
from_polars(schema, *, mode='coerce', fallback_type=None, name, version, description=None)
¶
Load a spec from a polars.Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
An instance of |
required |
mode
|
Literal['raise', 'coerce']
|
Loading mode. "raise" will raise exceptions on unsupported features. "coerce" will attempt to coerce unsupported features to supported ones with warnings. Defaults to "coerce". |
'coerce'
|
fallback_type
|
YadsType | None
|
A yads type to use as fallback when an unsupported Polars type is encountered. Only used when mode is "coerce". Must be either String or Binary, or None. Defaults to None. |
None
|
name
|
str
|
Fully-qualified spec name to assign. |
required |
version
|
int
|
Spec version string. |
required |
description
|
str | None
|
Optional human-readable description. |
None
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
import polars as pl
schema = pl.Schema({"id": pl.Int64, "name": pl.Utf8})
spec = from_polars(schema, name="users", version=1)
Source code in src/yads/loaders/__init__.py
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 | |
from_postgresql(connection, table_name, *, schema='public', mode='coerce', fallback_type=None, name=None, version=1, description=None)
¶
Load a spec from a PostgreSQL table.
Queries PostgreSQL catalog tables (information_schema, pg_catalog) to extract complete table schema information including column types, constraints, defaults, and generated columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Any
|
A DBAPI-compatible PostgreSQL connection (e.g., psycopg2, psycopg, asyncpg in sync mode). |
required |
table_name
|
str
|
Name of the table to load. |
required |
schema
|
str
|
PostgreSQL schema name. Defaults to "public". |
'public'
|
mode
|
Literal['raise', 'coerce']
|
Loading mode. "raise" will raise exceptions on unsupported features. "coerce" will attempt to coerce unsupported features to supported ones with warnings. Defaults to "coerce". |
'coerce'
|
fallback_type
|
YadsType | None
|
A yads type to use as fallback when an unsupported PostgreSQL type is encountered. Only used when mode is "coerce". Must be either String or Binary, or None. Defaults to None. |
None
|
name
|
str | None
|
Spec name to assign. Defaults to "{schema}.{table_name}". |
None
|
version
|
int
|
Spec version integer. Defaults to 1. |
1
|
description
|
str | None
|
Optional human-readable description. |
None
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
import psycopg2
conn = psycopg2.connect("postgresql://localhost/mydb")
spec = from_postgresql(conn, "users", schema="public")
Source code in src/yads/loaders/__init__.py
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 | |
from_sqlserver(connection, table_name, *, schema='dbo', mode='coerce', fallback_type=None, name=None, version=1, description=None)
¶
Load a spec from a SQL Server table.
Queries SQL Server catalog views (INFORMATION_SCHEMA, sys.*) to extract complete table schema information including column types, constraints, defaults, identity columns, and computed columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Any
|
A DBAPI-compatible SQL Server connection (e.g., pyodbc, pymssql). |
required |
table_name
|
str
|
Name of the table to load. |
required |
schema
|
str
|
SQL Server schema name. Defaults to "dbo". |
'dbo'
|
mode
|
Literal['raise', 'coerce']
|
Loading mode. "raise" will raise exceptions on unsupported features. "coerce" will attempt to coerce unsupported features to supported ones with warnings. Defaults to "coerce". |
'coerce'
|
fallback_type
|
YadsType | None
|
A yads type to use as fallback when an unsupported SQL Server type is encountered. Only used when mode is "coerce". Must be either String or Binary, or None. Defaults to None. |
None
|
name
|
str | None
|
Spec name to assign. Defaults to "{catalog}.{schema}.{table_name}". |
None
|
version
|
int
|
Spec version integer. Defaults to 1. |
1
|
description
|
str | None
|
Optional human-readable description. |
None
|
Returns:
| Type | Description |
|---|---|
YadsSpec
|
A validated immutable |
Example
import pyodbc
conn = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};...")
spec = from_sqlserver(conn, "users", schema="dbo")
Source code in src/yads/loaders/__init__.py
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 | |