TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Cyantic – build complex objects from simple blueprints with pydantic

2 pointsby rorytbyrne5 months ago

1 comment

rorytbyrne5 months ago
I do neuro&#x2F;ai research, so I have to instantiate a lot of Tensors from YAML definitions (that&#x27;s how I define models declaratively). I built a middleware to simplify this, using intermediary pydantic models as &quot;blueprints&quot; for building complex objects during pydantic&#x27;s build process. It lets me pass parameters (e.g. mean and standard deviation) into `thing.model_validate(...)`, and get a fully-built Tensor in the appropriate field of the model.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;flywhl&#x2F;cyantic">https:&#x2F;&#x2F;github.com&#x2F;flywhl&#x2F;cyantic</a><p>---<p>Example:<p>If you write a blueprint like<p><pre><code> @blueprint(Tensor) class NormalTensor(Blueprint[Tensor]): mean: float std: float size: tuple[int, ...] def build(self) -&gt; Tensor: return torch.normal(self.mean, self.std, size=self.size) </code></pre> and a data class like<p><pre><code> class MyModel(CyanticModel): some_tensor: Tensor </code></pre> you can construct MyModel using mean and std instead of a Tensor:<p><pre><code> some_yaml = &quot;&quot;&quot;common: size: [3, 5] some_tensor: mean: 0.0 std: 0.1 size: @value:common.size &quot;&quot;&quot; my_model = MyModel.model_validate(yaml.safe_load(some_yaml)) </code></pre> You can also pre-process the data using `@hook` references, and there&#x27;s an API for defining custom hooks.<p>There&#x27;s good test coverage and I think the library is ready for use, but would appreciate issues if you come across any bugs.<p>Thanks for reading