Preserve JSON Schema references in flow parameters #23432
<!-- devin-review-comment {"id": "BUG_pr-review-job-a0f810747a7f47cb84fa636e61acbb88_0001", "file_path": "src/prefect/flows.py", "start_line": 613, "end_line": 614, "side": "RIGHT", "based_on_repo_rules": false, "kind": "bug"} -->
🟡 UUID URI references abort schema flows
When a schema uses urn:uuid:... in $ref, UUID(ref) treats it as a block ID. Without a matching saved block, Block.load_from_ref aborts the flow before it can use the schema.
<details>
<summary>Learn more</summary>
A JSON Schema $ref is a URI reference, so it can legally contain a UUID URN. Python's UUID constructor accepts urn:uuid:..., but Block.load_from_ref interprets that same string as a saved block ID and raises when no block exists. The flow therefore fails before its body receives a valid schema.
Example: A flow receives {"properties": {"item": {"$ref": "urn:uuid:12345678-1234-1234-1234-123456789abc"}}}. Validation queries for a block with ID 12345678-1234-1234-1234-123456789abc instead of passing the schema to the flow.
Recommended fix: Distinguish explicit block-reference syntax from URI references before invoking Block.load_from_ref. If plain UUID strings must remain supported, constrain that path to the serialized block-ID form and treat urn:uuid: as schema data; add a regression covering UUID URNs.
</details>
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
---
Was this helpful? React with 👍 or 👎 to provide feedback.
[!NOTE]
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 1 potential issue.
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Fixed in f4fdb44. String references containing a URI scheme now stay as parameter data before UUID parsing. Added regressions for both urn:uuid:... and uuid:...; each reproduced the block-resolution failure before the guard. An explicit block_document_id dictionary containing a UUID URN still loads its saved block.
The block-reference and flow suites pass: 401 passed, 11 skipped, 2 xfailed. Flow typing checks pass all 6 tests, and the applicable pre-commit hooks pass.
✅ Resolved: The colon guard now preserves UUID URNs as schema data, preventing the prior schema-flow failure. The current finding concerns the opposite case: a UUID URN intentionally supplied as a saved-block reference is no longer resolved.
<!-- devin-review-comment {"id": "BUG_pr-review-job-578700d7bdd746d0bbef6ec5bb1b46da_0001", "file_path": "src/prefect/flows.py", "start_line": 613, "end_line": 614, "side": "RIGHT", "based_on_repo_rules": false, "kind": "bug"} -->
🔴 UUID URN block references stop loading
When a flow receives {"$ref": block_id.urn}, resolve_block_reference preserves the dictionary instead of loading the block. Block's ID reader accepts URNs through UUID, so block parameters now fail validation.
<details>
<summary>Learn more</summary>
A flow parameter can use a saved block document's UUID string as its $ref. Python's UUID() also parses the ID's urn:uuid: and uuid: forms. Block's ID reader uses that parser, so it can load those references. The colon guard returns the original dictionary before block loading, and a flow parameter annotated with a Block then fails validation.
Example: Save a ParamBlock with ID 12345678-1234-1234-1234-123456789abc. Passing {"$ref": "urn:uuid:12345678-1234-1234-1234-123456789abc"} to a flow with a ParamBlock parameter now leaves $ref as input rather than loading that block.
Recommended fix: Distinguish schema URI references from block references without excluding UUID URNs recognized by Block.load_from_ref. Add a direct $ref URN block test alongside the existing dictionary block_document_id URN test, while retaining the schema URI preservation test.
</details>
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
---
Was this helpful? React with 👍 or 👎 to provide feedback.
[!NOTE]
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 1 new potential issue.
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
This is a real compatibility change. The proposed boundary treats direct URI references as schema data and retains UUID URNs for blocks through the explicit block_document_id dictionary. The PR description now calls this out.
The visitor sees parameter values without field paths or expected Python types, so identical {"$ref": "urn:uuid:..."} inputs cannot mean both schema and block for Any, dictionary, or untyped parameters. Looking up a block and falling back on failure would make schema behavior depend on database contents. Handling only top-level Block annotations would leave nested containers and models inconsistent.
Could a maintainer confirm this syntax boundary, or whether preserving all implicit UUID spellings is required? The latter needs a broader resolution design. The explicit-ID URN regression passes; this thread remains open for that decision.
<!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
Merging this PR will not alter performance
<details>
<summary>:warning: <b>5 benchmarks spent significant time in system calls</b></summary>
System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.
> Measurement and system calls
> - [`bench_import_module[prefect.flows]](https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?uri=benches%2Fbench_import.py%3A%3Abench_import_module%255Bprefect.flows%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark)
- [bench_import_module[prefect.client.schemas.objects]](https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?uri=benches%2Fbench_import.py%3A%3Abench_import_module%255Bprefect.client.schemas.objects%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark)
- [bench_import_module[prefect.task_runners]](https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?uri=benches%2Fbench_import.py%3A%3Abench_import_module%255Bprefect.task_runners%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark)
- [bench_import_module[prefect.deployments]](https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?uri=benches%2Fbench_import.py%3A%3Abench_import_module%255Bprefect.deployments%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark)
- [bench_import_module[prefect.events]](https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?uri=benches%2Fbench_import.py%3A%3Abench_import_module%255Bprefect.events%255D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark)
</details>
✅ 8 untouched benchmarks ⏩ 1` skipped benchmark[^skipped]
---
<sub>Comparing <code>ska2704:fix/flow-json-schema-references</code> (4bb84af) with <code>main</code> (817bc93)</sub>
<a href="https://app.codspeed.io/PrefectHQ/prefect/branches/ska2704%3Afix%2Fflow-json-schema-references?utm_source=github&utm_medium=comment-v2&utm_content=button">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
<img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
</picture>
</a>
[^skipped]: 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.
Please preserve the existing direct UUID-URN block references while fixing schema handling. I verified that a saved block passed as {"$ref": block_id.urn} loads on the merge base but fails parameter validation on this head. Add that direct-reference regression alongside the schema-preservation tests; the explicit block_document_id case does not cover existing callers.
Please address the backward-compatibility regression discussed in the inline thread before merging.
Restored direct UUID-URN block references in 4bb84af. The new uuid_urn and uuid_uri cases pass on the merge base, fail on f4fdb44 with missing block fields, and pass after removing the colon guard. The explicit block_document_id URN case remains covered.
UUID-parseable strings now retain their existing block meaning. I replaced the two conflicting UUID-URI schema expectations with a non-UUID URN case; local paths, relative references, URLs, and schemas inside Pydantic models remain covered. The docstring and PR description state that UUID-shaped schema references keep their legacy block interpretation.
The flow/block suite passes with 402 passed, 11 existing skips and 2 xfails. All 6 flow typing checks and the applicable commit/push hooks pass.
✅ Resolved: The prior issue was that UUID URN block references were preserved instead of loaded. The current code removes the colon guard, so direct UUID URN block references now resolve; this item is resolved.
<!-- devin-review-comment {"id": "BUG_pr-review-job-05bb9306e9dc44c7abf0d1d146bfbe5d_0001", "file_path": "src/prefect/flows.py", "start_line": 613, "end_line": 614, "side": "LEFT", "based_on_repo_rules": false, "kind": "bug"} -->
🔴 UUID schema references break flow parameters
When schema data contains a urn:uuid: or uuid: reference, UUID(ref) treats it as a block ID. Block.load_from_ref then fails for an unsaved UUID or replaces the schema reference with a saved block.
(Refers to this code)
<details>
<summary>Learn more</summary>
Flow parameters are traversed recursively by visit_collection. The resolver treats strings accepted by Python's UUID parser as saved block IDs. Python also accepts urn:uuid: and uuid: strings, which can be valid URI references inside JSON Schema. Block.load_from_ref raises when a referenced block does not exist, and the flow converts that failure to ParameterTypeError before executing. If the UUID belongs to a saved block, the reference is replaced instead.
Example: A flow accepting dict[str, Any] receives {"properties": {"item": {"$ref": "urn:uuid:12345678-1234-1234-1234-123456789abc"}}}. Without a saved block at that ID, validation fails instead of passing the schema unchanged.
Recommended fix: Preserve URI-form $ref strings as schema data, and require an explicit block_document_id dictionary for URI-form block IDs. Restore a regression test for both urn:uuid: and uuid: schema references.
</details>
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
---
Was this helpful? React with 👍 or 👎 to provide feedback.
Devin Review found 1 new potential issue.
<!-- devin-review-badge-begin -->
<a href="https://app.devin.ai/review/prefecthq/prefect/pull/23432" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
This is the existing UUID-reference ambiguity documented in the PR body and docstring. The merge base also interprets these strings as block IDs. The maintainer explicitly requested preserving direct UUID-URN block references in https://github.com/PrefectHQ/prefect/pull/23432#discussion_r4315450645, after confirming that the URI guard broke existing callers.
Requiring an explicit dictionary here would restore that rejected compatibility change. The new direct-reference regressions now pass on both the merge base and this head, while the schema tests cover local paths, relative references, URLs and non-UUID URNs. Distinguishing UUID-shaped schemas from the same block-reference syntax needs a separate design. Resolving this finding against the current backward-compatibility requirement.
ska2704
closes #15763
This PR preserves non-block
$refvalues in flow parameters. A schema containing#/$defs/Itemcurrently fails withParameterTypeErrorbefore the flow runs, including when the schema is nested inside a Pydantic model. Local paths, relative references, URLs and non-UUID URNs now pass through as parameter data.UUID objects, all strings accepted by the UUID parser, and dictionaries containing
block_document_idorblock_document_slugstill reachBlock.load_from_ref. This preserves direct UUID-URN block references and errors for malformed explicit block IDs. UUID-parseable schema references retain their existing block interpretation; this change does not introduce a separate schema discriminator. Thevalidate_parametersdocstring records that boundary.<details>
<summary>Validation</summary>
urn:uuid:anduuid:block regressions pass on the merge base (87b5243), fail on the previous PR head (f4fdb44), and pass with this revision.uv run pytest tests/blocks/test_block_reference.py tests/test_flows.py -q -n 4: 402 passed, 11 existing skips, 2 expected failures.uv run pytest tests/typesafety/test_flows.yml -q: 6 passed.uv run pre-commit run --files src/prefect/flows.py tests/blocks/test_block_reference.py: all applicable hooks passed.Coverage includes direct and explicit UUID/URN block references, slug references, malformed explicit block IDs, non-block schema references, and schemas inside models passed to sync and async flows. Tests use the local test server; no Prefect Cloud account is needed.
</details>
Checklist
<link to issue>"- If no issue exists and your change is not a small fix, please create an issue first.
mint.json.