Skip to content

Pydantic issues after #19119 due to forwardref #19288

@ogenstad

Description

@ogenstad

Bug summary

After #19119 was merged we're seeing some issues with Flow.validate_parameters()

The problem is that we're sending along pydantic models as part of the parameter and as such run into issues such as:

2025-10-27T12:09:49.972149Z [error    ] Unhandled exception occurred in resolvers [infrahub]
Traceback (most recent call last):
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/graphql/execution/execute.py", line 530, in await_result
    return_type, field_nodes, info, path, await result
                                          ^^^^^^^^^^^^
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/opentelemetry/util/_decorator.py", line 69, in async_wrapper
    return await func(*args, **kwargs)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrick/Code/opsmill/infrahub/backend/infrahub/graphql/mutations/branch.py", line 93, in mutate
    await graphql_context.active_service.workflow.execute_workflow(
  File "/Users/patrick/Code/opsmill/infrahub/backend/infrahub/services/adapters/workflow/local.py", line 30, in execute_workflow
    parameters = flow_func.validate_parameters(parameters=parameters)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/prefect/flows.py", line 652, in validate_parameters
    return validated_fn.validate_call_args(args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/prefect/_internal/pydantic/validated_func.py", line 297, in validate_call_args
    validated = self.model.model_validate(values)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/pydantic/main.py", line 627, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrick/.virtualenvs/infrahub/lib/python3.12/site-packages/pydantic/_internal/_mock_val_ser.py", line 100, in __getattr__
    raise PydanticUserError(self._error_message, code=self._code)
pydantic.errors.PydanticUserError: `Create_BranchModel` is not fully defined; you should define `BranchCreateModel`, then call `Create_BranchModel.model_rebuild()`.

The issue seems to be that a models is dynamically generated here:

https://github.com/PrefectHQ/prefect/blob/3.4.25/src/prefect/_internal/pydantic/validated_func.py#L151-L227

Then within this context the models we're sending in as parameters doesn't exist. If I add these inline imports at the top of the _create_model() method it works.

        from infrahub.context import InfrahubContext
        from infrahub.graphql.mutations.models import BranchCreateModel

The above is of course just an example to highlight the issue.

Likewise if I rebuild the model and instead end _create_model() with this it also works and could be a fix:

        self.model.model_rebuild(_types_namespace=self.raw_function.__globals__)

I'm not completely sure about the overhead of this when, i.e. how resource intensive that .model_rebuild is or the impact of always creating a new model on the fly.

Version info

❯ prefect version
Version:              3.4.25
API version:          0.8.4
Python version:       3.12.11
Git commit:           8a37e7b1
Built:                Thu, Oct 23, 2025 07:58 PM
OS/Arch:              darwin/arm64
Profile:              local
Server type:          server
Pydantic version:     2.10.6
Server:
  Database:           postgresql
  PostgreSQL version: 16.10
Integrations:
  prefect-client:     3.4.25
  prefect-redis:      0.2.6

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions