Skip to main content

Render valbridge IR into Pydantic v2 models

Project description

valbridge-pydantic

Pydantic v2 adapter for valbridge -- converts JSON Schema into native Pydantic BaseModel classes with full type safety.

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. Define schemas in a config file:

// 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.0.2.tar.gz (61.4 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.0.2-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: valbridge_pydantic-1.0.2.tar.gz
  • Upload date:
  • Size: 61.4 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.0.2.tar.gz
Algorithm Hash digest
SHA256 31430523011dcd6a1679ab490a88df9964d8795c6ef8f54c69b27291f61094a2
MD5 884db926a4be9fd4abecb53d7b3b2746
BLAKE2b-256 78bf8bc67ac59f5021a9caec0054b4194cf5a407dc37d57b62b733d230128af9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for valbridge_pydantic-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77d280ced61455a27856163e86402bd992fd10dce386731c80f209de82745c20
MD5 3dd978f2317be0446934f054b1e0c2bb
BLAKE2b-256 8182988e000c2dcdc030996eb1466d172762d078a1d8eb9b136be6745cc46ae6

See more details on using hashes here.

Provenance

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