Skip to main content

Derive Optuna distributions from constrained Pydantic models.

Project description

pydantic-optuna-bridge

Derive Optuna distributions directly from constrained Pydantic models. This project extracts the metadata helpers originally used in the kaggle-map repository so that the utilities can be reused across projects.

Installation

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

Install the optional CLI dependencies if you plan to use the demo command:

uv add pydantic-optuna-bridge[cli]

Usage

Create a Pydantic model that constrains your hyperparameters and derive the Optuna metadata:

from enum import Enum
from typing import Annotated

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

from pydantic_optuna_bridge import attach_optuna_metadata, derive_optuna_metadata


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


class TrainingConfig(BaseModel):
    optimizer: Optimizer
    learning_rate: Annotated[float, Gt(1e-5), Lt(1.0)]
    hidden_units: Annotated[int, Ge(32), Le(256)]


metadata = derive_optuna_metadata(
    TrainingConfig,
    log_scale_fields={"learning_rate"},
    categorical_field_weights={"optimizer": [0.5, 0.3, 0.2]},
)
attach_optuna_metadata(TrainingConfig, metadata)

metadata now contains Optuna-compatible distributions and the model fields carry the same data inside json_schema_extra for downstream consumers.

CLI

The package ships with a tiny demo CLI for smoke-testing constrained models:

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

Development

uv run pytest
uv build

Pull requests and suggestions are welcome.

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

pydantic_optuna_bridge-0.1.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

pydantic_optuna_bridge-0.1.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_optuna_bridge-0.1.0.tar.gz.

File metadata

File hashes

Hashes for pydantic_optuna_bridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bcc9f9cbf71d79c59a136e3669b366ef5c2cf2479d8b65a40def33ebca5676f0
MD5 b171a39a317902e9fbcb3170558a4862
BLAKE2b-256 13b74cd0bf60581c24d78a6d87276499e74af886fda25d06b96e2c3e892fbfea

See more details on using hashes here.

File details

Details for the file pydantic_optuna_bridge-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_optuna_bridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ab0f61e00fd0d4a92b376db10d99e572b73c31c4752fb187c95f41aca99dcca
MD5 32cd6a7bb2825c478ff3dff78964852b
BLAKE2b-256 1386449592d80da50d533da4194414b1e39ee269e25b2606b2db94e94f632799

See more details on using hashes here.

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