Skip to content

Make declarative refs work across flows and crews#6326

Merged
vinibrsl merged 1 commit into
mainfrom
single-ref-loading
Jun 24, 2026
Merged

Make declarative refs work across flows and crews#6326
vinibrsl merged 1 commit into
mainfrom
single-ref-loading

Conversation

@vinibrsl

@vinibrsl vinibrsl commented Jun 24, 2026

Copy link
Copy Markdown
Member

Declarative flows already used module:qualname refs for runtime objects, but crew JSON tools still had their own lookup path. That meant examples like project_tools:LookupTool were treated as named crewai_tools lookups and failed with guidance that only mentioned SerperDevTool or custom:<name>. Invalid refs such as not_tools:NotATool also missed the same BaseTool validation used by flow tool actions.

Move ref resolution into a shared declarative helper, use it from flow tool actions and crew JSON loading, and require tool refs to resolve to BaseTool classes before instantiation. Validation still checks tool refs structurally, so validating a crew does not import or execute project code.


Note

Medium Risk
Shared ref helpers import user modules at load/runtime; behavior changes how crew JSON tool strings with colons are interpreted, though validation avoids executing tool import side effects.

Overview
Unifies module:qualname reference handling so declarative flows and JSON crew projects share one contract instead of parallel lookup logic.

Ref resolution moves from flow/runtime/_refs.py into crewai.utilities.declarative_refs (resolve_ref, resolve_class_ref, InvalidRefError). Flow runtime keeps auto-instantiation for providers via a local _resolve_instance_ref. Flow tool actions now use resolve_class_ref with a BaseTool subclass check before no-arg instantiation.

Crew JSON tool strings that contain : (e.g. project_tools:LookupTool) are resolved as import refs with the same BaseTool validation and instantiation errors as flow tools—not as bare crewai_tools class names. Unknown-tool guidance mentions module:ClassName alongside SerperDevTool and custom:<name>.

Tests add success/failure paths for import-based tools and assert validate_crew_project does not import tool refs (structural validation only).

Reviewed by Cursor Bugbot for commit 2ae2bda. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • New Features

    • Added support for module:ClassName import references in tool configuration.
    • Improved reference resolution for provider/action definitions used at runtime.
  • Bug Fixes

    • More consistent, field-aware errors when references can’t be resolved or valid classes can’t be instantiated.
    • Strengthened project validation to avoid importing tool code and triggering side effects.
  • Tests

    • Expanded JSON tool loader tests for successful and failing module:ClassName cases.
    • Added coverage to confirm validation does not execute tool imports.

@corridor-security corridor-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary: This PR centralizes declarative reference resolution and extends crew JSON tool loading to support module:ClassName refs with BaseTool subclass validation.

Risk: Low risk. No exploitable security vulnerabilities were identified; the added dynamic import path is tied to project configuration/runtime tool loading and includes class/type validation, while validation paths avoid importing project code.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e5526b1f-a409-41b3-95e8-a3be8eeec28a

📥 Commits

Reviewing files that changed from the base of the PR and between 096586d and 2ae2bda.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/src/crewai/flow/runtime/_actions.py
  • lib/crewai/src/crewai/flow/runtime/_refs.py
  • lib/crewai/src/crewai/project/json_loader.py
  • lib/crewai/src/crewai/utilities/declarative_refs.py
  • lib/crewai/tests/project/test_json_loader.py
💤 Files with no reviewable changes (1)
  • lib/crewai/src/crewai/flow/runtime/_refs.py
✅ Files skipped from review due to trivial changes (1)
  • lib/crewai/src/crewai/utilities/declarative_refs.py
🚧 Files skipped from review as they are similar to previous changes (4)
  • lib/crewai/src/crewai/flow/runtime/init.py
  • lib/crewai/src/crewai/flow/runtime/_actions.py
  • lib/crewai/src/crewai/project/json_loader.py
  • lib/crewai/tests/project/test_json_loader.py

📝 Walkthrough

Walkthrough

Adds a shared module:qualname resolver, updates runtime provider and tool resolution to use it, and extends JSON tool loading to instantiate imported tool classes. Tests cover import-ref resolution, failure cases, and validation behavior.

Changes

Declarative refs and import-ref tools

Layer / File(s) Summary
Shared ref resolver contract
lib/crewai/src/crewai/utilities/declarative_refs.py
Adds InvalidRefError, resolve_ref, and resolve_class_ref for module:qualname references.
Runtime consumers
lib/crewai/src/crewai/flow/runtime/__init__.py, lib/crewai/src/crewai/flow/runtime/_actions.py
Flow input providers, human feedback providers, and tool do: refs resolve through the shared helpers.
JSON tool import refs
lib/crewai/src/crewai/project/json_loader.py, lib/crewai/tests/project/test_json_loader.py
Project tool loading accepts module:ClassName refs, instantiates BaseTool subclasses, and adds validation coverage for success and failure cases.

Sequence Diagram(s)

sequenceDiagram
  participant json_loader._resolve_tools
  participant json_loader._instantiate_tool_import_ref
  participant resolve_class_ref
  participant project_tools.LookupTool
  json_loader._resolve_tools->>json_loader._instantiate_tool_import_ref: tools entry "project_tools:LookupTool"
  json_loader._instantiate_tool_import_ref->>resolve_class_ref: resolve BaseTool subclass
  resolve_class_ref->>project_tools.LookupTool: import module:qualname
  resolve_class_ref-->>json_loader._instantiate_tool_import_ref: validated class
  json_loader._instantiate_tool_import_ref-->>json_loader._resolve_tools: instantiated tool
Loading

Suggested reviewers

  • joaomdmoura
  • alex-clawd
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.79% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: unifying declarative reference handling across flow and crew code paths.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch single-ref-loading

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
lib/crewai/src/crewai/project/json_loader.py (1)

1857-1866: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

Error mapping is coupled to resolve_ref/resolve_class_ref message text.

The branch keys off message.startswith("unresolvable ") and the literal "expected 'module:qualname'" substring. Any future wording change in declarative_refs will silently reclassify those errors as "expected a BaseTool class", producing misleading guidance. Consider distinguishing the cases structurally (e.g. dedicated exception subclasses or an error code on InvalidRefError) rather than string sniffing.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/project/json_loader.py` around lines 1857 - 1866, The
error mapping in the InvalidRefError handler is relying on
resolve_ref/resolve_class_ref message text, which is brittle and can misclassify
future errors. Update the JSONProjectError translation in json_loader to
distinguish unresolvable references from invalid class references using
structured data such as dedicated InvalidRefError subclasses or an error
code/flag on InvalidRefError, and then branch on that instead of checking string
prefixes/substrings before raising the appropriate JSONProjectError message.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/crewai/src/crewai/project/json_loader.py`:
- Around line 1857-1866: The error mapping in the InvalidRefError handler is
relying on resolve_ref/resolve_class_ref message text, which is brittle and can
misclassify future errors. Update the JSONProjectError translation in
json_loader to distinguish unresolvable references from invalid class references
using structured data such as dedicated InvalidRefError subclasses or an error
code/flag on InvalidRefError, and then branch on that instead of checking string
prefixes/substrings before raising the appropriate JSONProjectError message.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1c6bb7f9-816f-4a8b-9d30-940f4867e067

📥 Commits

Reviewing files that changed from the base of the PR and between 156b350 and 096586d.

📒 Files selected for processing (6)
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/src/crewai/flow/runtime/_actions.py
  • lib/crewai/src/crewai/flow/runtime/_refs.py
  • lib/crewai/src/crewai/project/json_loader.py
  • lib/crewai/src/crewai/utilities/declarative_refs.py
  • lib/crewai/tests/project/test_json_loader.py
💤 Files with no reviewable changes (1)
  • lib/crewai/src/crewai/flow/runtime/_refs.py

Declarative flows already used `module:qualname` refs for runtime
objects, but crew JSON tools still had their own lookup path. That meant
examples like `project_tools:LookupTool` were treated as named
`crewai_tools` lookups and failed with guidance that only mentioned
`SerperDevTool` or `custom:<name>`. Invalid refs such as
`not_tools:NotATool` also missed the same BaseTool validation used by
flow tool actions.

Move ref resolution into a shared declarative helper, use it from flow
tool actions and crew JSON loading, and require tool refs to resolve to
`BaseTool` classes before instantiation. Validation still checks tool
refs structurally, so validating a crew does not import or execute
project code.
@vinibrsl vinibrsl force-pushed the single-ref-loading branch from 096586d to 2ae2bda Compare June 24, 2026 21:39

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2ae2bda. Configure here.

Comment thread lib/crewai/src/crewai/project/json_loader.py
@vinibrsl vinibrsl merged commit 7738a1d into main Jun 24, 2026
56 checks passed
@vinibrsl vinibrsl deleted the single-ref-loading branch June 24, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants