Skip to main content

Render valbridge IR into Pydantic v2 models

Project description

valbridge-pydantic

Pydantic v2 adapter for valbridge -- generates native Pydantic BaseModel classes for valbridge's Zod/Pydantic conversion pipeline.

PyPI License


Installation

pip install valbridge-pydantic pydantic
# or
uv add valbridge-pydantic pydantic

Verified against pydantic 2.12.5.

Usage

This adapter is invoked by the valbridge CLI. It is typically used when converting from Zod or other valbridge-supported schema sources into Pydantic models:

// user.valbridge.jsonc
{
  "$schema": "https://github.com/vectorfy-co/valbridge/schemas/python.jsonc",
  "schemas": [
    {
      "id": "User",
      "adapter": "vectorfyco/valbridge-pydantic",
      "sourceType": "file",
      "source": "./schemas/user.json"
    }
  ]
}

Then generate:

valbridge generate

Use the generated models with the valbridge runtime client.

Verification

Run from the adapter directory (python/packages/adapters/pydantic/):

# JSON Schema Test Suite compliance (requires Go CLI)
cd ../../cli && go build -o valbridge . && \
  ./valbridge compliance --lang python --adapter-path ../python/packages/adapters/pydantic

# Unit tests
uv run pytest

# Type checking
uv run pyright src/

Fallback typing guardrails

The adapter uses Annotated[Any, BeforeValidator(...)] only for constructs where the validated domain is unbounded. Every other construct must produce a narrower type.

Allowed Any fallbacks

Construct Reason
not Negation is "everything except X" -- no union can express this
conditional (single branch) Unmatched values pass through unconstrained
typeGuarded (heterogeneous) Unmatched types pass through the guard
Open tuple Extra elements beyond prefix items are untyped
Recursive refs Self-referencing $ref cycles

Constructs that must NOT use Any

  • oneOf -- must produce T1 | T2 | ...
  • conditional (if/then/else) -- must produce ThenType | ElseType
  • Closed tuples -- must produce tuple[T1, T2, ...]
  • const / enum -- must produce the narrowest applicable type
  • allOf (object merges) -- must produce a BaseModel subclass

Disallowed patterns

  • Unknown IR node kind -- must raise ConversionError
  • Unresolved external refs -- must raise ConversionError
  • New renderer branches must not silently return Any without documented justification

Troubleshooting

  • pyright errors -- adapter source must pass uv run pyright src/ with zero errors
  • Compliance failures -- run full compliance and compare against baseline; check that validator lambdas are functionally identical

Related packages

Package Purpose
valbridge Runtime client for generated validators
valbridge-core Core IR and JSON Schema parser
valbridge-pydantic-bridge Bridge helpers for Pydantic generation
@vectorfyco/valbridge-zod TypeScript equivalent (Zod adapter)

Learn more

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

valbridge_pydantic-1.2.0.tar.gz (61.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

valbridge_pydantic-1.2.0-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file valbridge_pydantic-1.2.0.tar.gz.

File metadata

  • Download URL: valbridge_pydantic-1.2.0.tar.gz
  • Upload date:
  • Size: 61.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for valbridge_pydantic-1.2.0.tar.gz
Algorithm Hash digest
SHA256 b7226dcb63ebdc543596123bfbc11b3d014c61cc55c50d6ed756ce90e45fc348
MD5 3470b6fe0787f33aab3fe775989de6ae
BLAKE2b-256 1846e67934df9886ad4b2a1c21a3b18554aacb084c115f6752a1ca52efe1858c

See more details on using hashes here.

Provenance

The following attestation bundles were made for valbridge_pydantic-1.2.0.tar.gz:

Publisher: release-please.yml on vectorfy-co/valbridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file valbridge_pydantic-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for valbridge_pydantic-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b03fe9e82d5ef3103ad6af1dce5aa373b2ecc856d6e252362687cd950baee606
MD5 93a3d6c1cb749d19c1bea0deedcbdf70
BLAKE2b-256 d739bee037dad4d5d312217fca48395090386c9f827071c94884f6daed22e47b

See more details on using hashes here.

Provenance

The following attestation bundles were made for valbridge_pydantic-1.2.0-py3-none-any.whl:

Publisher: release-please.yml on vectorfy-co/valbridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page