Skip to content

Add unit-aware Param() field and with_units()/to_example() to meshly#24

Merged
afshawnlotfi merged 2 commits intomainfrom
feature/units
Apr 17, 2026
Merged

Add unit-aware Param() field and with_units()/to_example() to meshly#24
afshawnlotfi merged 2 commits intomainfrom
feature/units

Conversation

@afshawnlotfi
Copy link
Copy Markdown
Member

Summary

Adds Param(), a drop-in replacement for pydantic.Field() that carries units, shape, and example metadata through to the JSON schema. Also adds two new methods on Packable: to_example() (build an instance from example/default values) and with_units() (clone with numeric fields converted to pint.Quantity objects). This enables downstream packages like hypersim to declare physical units on simulation parameters and have them appear in tool schemas, MCP integrations, and UI forms without any manual filtering or schema post-processing.

Usage Example

from meshly import Packable, Param, InlineArray

class Simulation(Packable):
    velocity: InlineArray = Param(units="m/s", example=[30.0, 0, 0], shape=(3,))
    temperature: float = Param(300.0, units="K", description="Fluid temperature")
    pressure: float = Param(101325.0, units="Pa")

# Units in JSON schema
schema = Simulation.model_json_schema()
# schema["properties"]["temperature"]["units"] == "K"

# Build from examples
sim = Simulation.to_example()
# sim.velocity == np.array([30., 0., 0.])

# Convert to pint Quantities
sim_units = sim.with_units()
# sim_units.velocity == Quantity([30. 0. 0.], 'm/s')
# sim_units.velocity.to("km/h") == Quantity([108. 0. 0.], 'km/h')

@afshawnlotfi afshawnlotfi merged commit cf8cef8 into main Apr 17, 2026
3 checks passed
afshawnlotfi added a commit that referenced this pull request Apr 17, 2026
…eshly (#24)

* Add unit-aware parameter support with Param() and update README for installation instructions

* Bump version to 3.5.0-alpha in pyproject.toml
@afshawnlotfi afshawnlotfi deleted the feature/units branch April 17, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant