Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic should default to v2, or hera should allow it to be configured #984

Open
2 of 7 tasks
terrykong opened this issue Feb 26, 2024 · 6 comments
Open
2 of 7 tasks

Comments

@terrykong
Copy link

Pre-bug-report checklist

1. This bug can be reproduced using pure Argo YAML

If yes, it is more likely to be an Argo bug unrelated to Hera. Please double check before submitting an issue to Hera.

2. This bug occurs in Hera when...

  • exporting to YAML
  • submitting to Argo
  • running on Argo with the Hera runner
  • other: when using hera's pydantic models

Bug report

Describe the bug
A clear and concise description of what the bug is:
When mixing in hera models into my own pydantic models, I get errors like this:

         return cls.model_validate(kwargs, strict=True)
E       TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given

Because I'm using the default in pydantic (V2 models), but hera defaults to the v1 models:

I think this is a bug b/c _PYDANTIC_VERSION gets set to 2 even though the imports are v1.

Error log if applicable:

To Reproduce
Full Hera code to reproduce the bug:

python -c 'from hera.workflows.models import Volume; print(Volume.__mro__)'
(<class 'hera.workflows.models.io.k8s.api.core.v1.Volume'>, <class 'hera.shared._pydantic.BaseModel'>, <class 'pydantic.v1.main.BaseModel'>, <class 'pydantic.v1.utils.Representation'>, <class 'object'>)

Expected behavior
A clear and concise description of what you expected to happen:

I think this should default to import pydantic.v2 first, but given that both versions are supported according to comments in _pydantic.py, I think this could also be a feature request in that there should be some variable like HERA_PYDANTIC_API that defaults to v2, but lets others choose v1 if they're still stuck on that version.

Environment

  • Hera Version: 5.14.0
  • Python Version: 3.X.X
  • Argo Version: 3.X.X

Additional context
Add any other context about the problem here.

@elliotgunton
Copy link
Collaborator

Hi @terrykong, thanks for adding the issue. The original PR #795 to add V2 has some history.

The problem is specifically that Hera's codebase was written using Pydantic V1, including the generated Argo model classes. This would require a fairly large time investment to provide models for both versions, and increase the maintenance burden for the two codepaths throughout the codebase (@samj1912 explored this initially but decided against it). Long term we do plan to migrate the codebase to V2, but it should not affect end users at this time.

I'm curious why you would need to mix Hera models with your own? As Hera builds workflows into yaml I'm not sure what missing feature your own V2 "mixin" models would be providing?

Also, the script runner feature lets you use Pydantic V1 or V2 models within your script template functions.

@elliotgunton elliotgunton added the note:needs-info We need more info from the issue submitter label Mar 5, 2024
@terrykong
Copy link
Author

Hi @elliotgunton

So in my use case, I was authoring configuration that used pydantic's BaseModel, but tried to add in hera types as fields, and ran into this issue. So something like this:

from hera.workflows.models import Volume
from pydantic import BaseModel

class MyConfig(BaseModel):
  foobar: str
  volume: Volume

MyConfig.model_validate({"foobar":"a", "volume": {"host_path": {"path": "/mnt"}}})
# Gives
'''
File "/.../venv/lib/python3.10/site-packages/pydantic/main.py", line 509, in model_validate
    return cls.__pydantic_validator__.validate_python(
'''

class H(BaseModel):
  path: str

class V(BaseModel):
  host_path: H

class MyConfigWhichDoesWork(BaseModel):
  foobar:str
  volume: V

MyConfigWhichDoesWork.model_validate({"foobar":"a", "volume": {"host_path": {"path": "/mnt"}}})
# And this works

So basically I want to use pydantic with hera's types but I think the v1 v2 API mismatch is causing pydantic's cryptic error message.

@elliotgunton
Copy link
Collaborator

Thank you @terrykong! Using the Hera classes as part of your configs makes sense 👍 The point remains that it will take a significant effort to allow both v1 and v2 classes to be exported from the library, so I can't give an expected timeframe/exact details for the time being.

@elliotgunton elliotgunton removed the note:needs-info We need more info from the issue submitter label Mar 12, 2024
@dev-goyal

This comment was marked as resolved.

@elliotgunton

This comment was marked as resolved.

@dev-goyal

This comment was marked as resolved.

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

No branches or pull requests

3 participants