Sneak peek: pydantic parsing & validation for Hydra apps #665
rsokl
started this conversation in
Show and tell
Replies: 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Edit: check it out! https://mit-ll-responsible-ai.github.io/hydra-zen/changes.html#rc1-2024-04-01
tl;dr:
hydra_zen.instantiate
is going to have a custom validation-wrapper argument. And hydra-zen will also provide a pydantic wrapper that will enable pydantic's rich parsing capabilities for converting/validating arguments on all config-instantiation calls. This is very powerful. It is basically Hydra's CLI x pydantic's parsing & validation.Really excited about this. I figured out how to add a custom validation layer to
instantiate
.Typically if you have
cfg = builds(target, **kwargs)
, callinginstantiate(cfg)
ultimately callstarget(**kwargs)
. Now you will be able to callinstantiate(cfg, target_wrapper=w)
, and it will callw(target)(**kwargs)
. Critically, this works recursively -- applyingw
within all recursiveinstantiate
calls, and it doesn't change any of your configs.Who cares?
Well, if you set
target_wrapper=pydantic.validate_call
, this means that you get pydantic's parsing and validation logic across all instantiated configs!So what?
Now you get much more sophisticated runtime type-checking. E.g. support for
Literal
, which Hydra doesn't support.You also get annotation-based parsing & conversion. E.g., Hydra is only ever able to produce list-type sequences from the CLI, but now we can be more sophisticated:
Given:
Before:
After:
You also get to use
pydantic
types in your annotations, and now you can validate against them. E.g.This is great. Hydra's type checking happens at the wrong layer of abstraction and is very bare bones. This moves the checking logic to the right place -- the point of instantiation -- and opts for pydantic's much more complete support for type annotations.
And obviously you don't have to use pydantic. You can use whatever validation/conversion logic you want here.
Beta Was this translation helpful? Give feedback.
All reactions