Derive Optuna distributions from constrained Pydantic models.
Project description
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
- Describe the search space in Pydantic.
- Decorate the model to wire Optuna metadata and helpers.
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_optuna_bridge-0.1.1.tar.gz.
File metadata
- Download URL: pydantic_optuna_bridge-0.1.1.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72e2129346161359b287c56726e4e51c4b0bbcfe5478215804ffd37a632a7143
|
|
| MD5 |
844e1953653decb19ccfe1841d07ecb6
|
|
| BLAKE2b-256 |
a7d62025a4cdde406997bced0f5697bb47f4db0ab2558574a88e488657053f11
|
File details
Details for the file pydantic_optuna_bridge-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_optuna_bridge-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c57539300dab5b1e54791853e74e8e9c0b7ebc3ff366097b80c1093212f217c3
|
|
| MD5 |
0effc500a549bd91ec33c518fc8de438
|
|
| BLAKE2b-256 |
46d13fd90c1814097295bb3e610b9cfa6c92a631f4182d9536f51bd7b2baf073
|