Skip to content

Conversation

@desertaxle
Copy link
Member

@desertaxle desertaxle commented Oct 27, 2025

Closes #19288

This PR fixes an issue where Flow.validate_parameters() fails when using Pydantic models as parameters with from __future__ import annotations.

Details

Problem

After #19119 was merged, users reported that Flow.validate_parameters() raised a PydanticUserError when:

  1. Using from __future__ import annotations (which makes type hints strings/forward references)
  2. Passing Pydantic models as flow parameters

The error was:

PydanticUserError: `RunModel` is not fully defined; you should define `MyObject`, then call `RunModel.model_rebuild()`.

Root Cause

In validated_func.py, the _create_model() method dynamically creates a Pydantic model to validate function parameters. However, this dynamically created model didn't have access to the original function's namespace to resolve forward references.

Solution

Added self.model.model_rebuild(_types_namespace=self.raw_function.__globals__) at the end of _create_model(). This provides the model with the original function's global namespace to properly resolve forward references to Pydantic models and other types.

🤖 Generated with Claude Code

Closes #19288

This PR fixes an issue where Flow.validate_parameters() fails when using
Pydantic models as parameters with `from __future__ import annotations`.

The problem occurred because the dynamically created validation model
didn't have access to the original function's namespace to resolve
forward references.

The fix adds model_rebuild() with the function's global namespace,
allowing proper resolution of forward references to Pydantic models.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-actions github-actions bot added the bug Something isn't working label Oct 27, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 27, 2025

CodSpeed Performance Report

Merging #19289 will not alter performance

Comparing fix-19288 (1810a4e) with main (7fcfb7e)

Summary

✅ 2 untouched

desertaxle and others added 3 commits October 27, 2025 09:19
This commit adds a performance optimization to avoid calling
model_rebuild() when there are no forward references in the function
signature.

The optimization:
- Detects string annotations during field building
- Only calls model_rebuild() if forward references are found
- Avoids unnecessary overhead for functions without forward refs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Improves test coverage by using unittest.mock to verify that:
- model_rebuild() is NOT called when there are no forward references
- model_rebuild() IS called with correct namespace when forward refs exist

This ensures the performance optimization is actually working as intended.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@desertaxle desertaxle marked this pull request as ready for review October 27, 2025 15:08
@desertaxle desertaxle changed the title Fix forward reference resolution in ValidatedFunction Fix forward reference resolution in ValidatedFunction Oct 27, 2025
Copy link
Collaborator

@zzstoatzz zzstoatzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@desertaxle desertaxle merged commit 916c204 into main Oct 27, 2025
65 of 66 checks passed
@desertaxle desertaxle deleted the fix-19288 branch October 27, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pydantic issues after #19119 due to forwardref

3 participants