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.1.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.1.0-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: valbridge_pydantic-1.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 d20eaae36aee4fdd62a4f50a4320e0b5b6b3378845e5895576c463879b081829
MD5 afd28de82f6a9713d340d802b8f55ac0
BLAKE2b-256 b9e53d2193dc8264a0be8f924527a6761af4c8855279aa9ac3c6325c285423e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for valbridge_pydantic-1.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for valbridge_pydantic-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7916256cf78ed836ad1403708ed40f035186dca39f45531315fbfbac98c3b996
MD5 cd87ed8250860ee6fa82621e0404e4d1
BLAKE2b-256 685770683c055364c90b82cb6d29d9b952164c6531ff4e6fe5658982cf2ad909

See more details on using hashes here.

Provenance

The following attestation bundles were made for valbridge_pydantic-1.1.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