Skip to main content

pydantic-optuna-bridge

Bridge constrained Pydantic models directly into Optuna search spaces. The package extracts the metadata helpers originally built for kaggle-map so that tightly scoped hyperparameter schemas can be reused across projects.

Installation

uv add pydantic-optuna-bridge
# or
pip install pydantic-optuna-bridge

# Optional CLI extras
uv add pydantic-optuna-bridge[cli]

End-to-End Usage with Optuna

  1. Describe the search space in Pydantic.
  2. Decorate the model to wire Optuna metadata and helpers.
  3. Sample configurations inside an Optuna objective.

Install Optuna alongside the bridge helpers:

uv add optuna
from enum import Enum
from typing import Annotated

import optuna
from annotated_types import Ge, Gt, Le, Lt
from pydantic import BaseModel

from pydantic_optuna_bridge import optuna_config


class Optimizer(str, Enum):
    ADAM = "adam"
    SGD = "sgd"
    RMSPROP = "rmsprop"


@optuna_config(
    log_scale_fields={"learning_rate"},
    categorical_field_weights={"optimizer": [0.5, 0.3, 0.2]},
)
class TrainingConfig(BaseModel):
    optimizer: Optimizer
    learning_rate: Annotated[float, Gt(1e-5), Lt(1.0)]
    hidden_units: Annotated[int, Ge(32), Le(256)]

def objective(trial: optuna.trial.Trial) -> float:
    config = TrainingConfig.from_optuna_trial(trial)

    # Replace with real training logic, keeping a toy signal for documentation.
    score = (
        0.1 if config.optimizer == Optimizer.ADAM else 0.2
    ) + 0.05 * abs(config.learning_rate - 1e-2) + config.hidden_units / 1_024
    return score


study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=5)
print(study.best_params)

Optuna now drives the search space defined by the Pydantic model. Because the decorator attaches metadata to json_schema_extra, downstream tools can inspect the schema without re-deriving it:

schema_payload = {
    name: field.json_schema_extra["optuna"]
    for name, field in TrainingConfig.model_fields.items()
}
assert schema_payload == TrainingConfig.optuna_metadata()

CLI Demo

The optional CLI showcases the metadata derivation and prints a formatted table for inspection:

uv run -m pydantic_optuna_bridge --help
uv run -m pydantic_optuna_bridge

Development

uv run pytest
uv build

Issues and pull requests are welcome.

Metadata

Release files for pydantic-optuna-bridge 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-optuna-bridge 0.1.1
File Size Uploaded
pydantic_optuna_bridge-0.1.1.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-optuna-bridge 0.1.1
File Interpreter ABI Platform
pydantic_optuna_bridge-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 28.6 kB

Release files / pydantic_optuna_bridge-0.1.1.tar.gz

Download URL pydantic_optuna_bridge-0.1.1.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
72e2129346161359b287c56726e4e51c4b0bbcfe5478215804ffd37a632a7143
BLAKE2b-256 checksum
How to use checksums
a7d62025a4cdde406997bced0f5697bb47f4db0ab2558574a88e488657053f11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.19

Release files / pydantic_optuna_bridge-0.1.1-py3-none-any.whl

Download URL pydantic_optuna_bridge-0.1.1-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c57539300dab5b1e54791853e74e8e9c0b7ebc3ff366097b80c1093212f217c3
BLAKE2b-256 checksum
How to use checksums
46d13fd90c1814097295bb3e610b9cfa6c92a631f4182d9536f51bd7b2baf073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.19

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page