Fix JSON crew template rendering#6359
Conversation
There was a problem hiding this comment.
Summary: This PR moves JSON crew scaffolding content into static template files and adds a simple placeholder renderer for CLI-generated project files. No exploitable security vulnerabilities were identified in the added code.
Risk: Low risk. The changes affect local CLI project generation and import timing, do not add public endpoints or authentication/authorization logic, and template values used in executable JSON crew definitions are JSON-encoded before insertion.
📝 WalkthroughWalkthroughThe PR switches JSON crew generation to file-based templates, adds a reusable placeholder renderer, and updates the runner to lazy-load project-definition helpers. Tests now cover template discovery, placeholder substitution, and older project-environment compatibility. ChangesJSON crew scaffolding
Runner imports
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 Biome (2.5.0)lib/cli/src/crewai_cli/templates/json_crew/agent_settings.jsoncFile contains syntax errors that prevent linting: Line 2: End of file expected; Line 2: End of file expected; Line 2: End of file expected; Line 5: End of file expected; Line 5: End of file expected; Line 5: Expected a property but instead found '{'.; Line 5: Property key must be double quoted; Line 5: expected lib/cli/src/crewai_cli/templates/json_crew/agent.jsoncFile contains syntax errors that prevent linting: Line 5: Expected a property but instead found '{'.; Line 5: Property key must be double quoted; Line 5: expected lib/cli/src/crewai_cli/templates/json_crew/crew.jsoncFile contains syntax errors that prevent linting: Line 3: Expected a property but instead found '{'.; Line 3: Property key must be double quoted; Line 3: expected
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. Comment |
JSON crews were not using existing CLI templates.
fd23dbb to
2dd335b
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
lib/cli/src/crewai_cli/run_crew.py (1)
111-132: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winOlder
crewai_corestill breaks the normalrun_crew()path.The older
crewai_core.projectstub introduced inlib/cli/tests/test_run_crew.pyLines 238-249 does not defineProjectDefinitionErrororconfigured_project_definition. Importing them here meansrun_crew()will still raiseImportErrorbefore it can fall back to the classic crew path, so the compatibility fix only covers the direct JSON runner subprocess path.Suggested guard
def configured_project_json_crew( pyproject_data: dict[str, Any] | None = None, project_root: Path | None = None, ) -> Path | None: """Return the configured JSON crew definition for crew projects.""" - from crewai_core.project import ( - ProjectDefinitionError, - configured_project_definition, - ) + from crewai_core import project as project_helpers + + configured_project_definition = getattr( + project_helpers, "configured_project_definition", None + ) + ProjectDefinitionError = getattr( + project_helpers, "ProjectDefinitionError", None + ) + if configured_project_definition is None or ProjectDefinitionError is None: + return None🤖 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/cli/src/crewai_cli/run_crew.py` around lines 111 - 132, The import in configured_project_json_crew still makes run_crew() fail with older crewai_core stubs because ProjectDefinitionError and configured_project_definition are imported unconditionally. Move the crewai_core.project import logic inside the function with a compatibility guard so missing symbols fall back cleanly to the classic crew path, and keep the existing click.UsageError conversion only when configured_project_definition is actually available.
🧹 Nitpick comments (1)
lib/cli/tests/test_run_crew.py (1)
230-301: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd coverage for the actual
run_crew()entrypoint.This subprocess only executes
_JSON_CREW_RUNNER_CODE, so it will not catch the remaining failure mode whererun_crew()callsconfigured_project_json_crew()under the same oldercrewai_corestub. A second subprocess that importscrewai_cli.run_crewand invokesrun_crew()would lock in the compatibility behavior this change is aiming for.🤖 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/cli/tests/test_run_crew.py` around lines 230 - 301, Add a subprocess test that exercises the actual run_crew() entrypoint, not just _JSON_CREW_RUNNER_CODE. Reuse the older crewai_core stub setup, then import crewai_cli.run_crew and call run_crew() so the test covers the configured_project_json_crew() path under legacy environments. Keep the existing marker-based assertion to verify the crew still kicks off successfully.
🤖 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.
Outside diff comments:
In `@lib/cli/src/crewai_cli/run_crew.py`:
- Around line 111-132: The import in configured_project_json_crew still makes
run_crew() fail with older crewai_core stubs because ProjectDefinitionError and
configured_project_definition are imported unconditionally. Move the
crewai_core.project import logic inside the function with a compatibility guard
so missing symbols fall back cleanly to the classic crew path, and keep the
existing click.UsageError conversion only when configured_project_definition is
actually available.
---
Nitpick comments:
In `@lib/cli/tests/test_run_crew.py`:
- Around line 230-301: Add a subprocess test that exercises the actual
run_crew() entrypoint, not just _JSON_CREW_RUNNER_CODE. Reuse the older
crewai_core stub setup, then import crewai_cli.run_crew and call run_crew() so
the test covers the configured_project_json_crew() path under legacy
environments. Keep the existing marker-based assertion to verify the crew still
kicks off successfully.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 6b780523-9909-4afa-9c58-c933cc70c03e
📒 Files selected for processing (13)
lib/cli/src/crewai_cli/create_json_crew.pylib/cli/src/crewai_cli/run_crew.pylib/cli/src/crewai_cli/templates/json_crew/.gitignorelib/cli/src/crewai_cli/templates/json_crew/README.mdlib/cli/src/crewai_cli/templates/json_crew/agent.jsonclib/cli/src/crewai_cli/templates/json_crew/agent_settings.jsonclib/cli/src/crewai_cli/templates/json_crew/crew.jsonclib/cli/src/crewai_cli/templates/json_crew/knowledge/user_preference.txtlib/cli/src/crewai_cli/templates/json_crew/pyproject.tomllib/cli/src/crewai_cli/templates/json_crew/task.jsonclib/cli/src/crewai_cli/utils.pylib/cli/tests/test_create_crew.pylib/cli/tests/test_run_crew.py
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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.
Inline comments:
In `@lib/cli/src/crewai_cli/create_json_crew.py`:
- Line 83: The JSON crew template directory referenced by create_json_crew.py
via _TEMPLATES_DIR is not guaranteed to be packaged, so installed CLI builds can
miss these files and raise FileNotFoundError. Update lib/cli/pyproject.toml to
explicitly include or force-include src/crewai_cli/templates/json_crew/** in the
package data so the templates are bundled with the CLI artifact.
In `@lib/cli/src/crewai_cli/templates/json_crew/agent.jsonc`:
- Around line 1-59: The template files under the json_crew generator are being
parsed as JSONC even though they still contain unrendered {{...}} placeholders,
so Biome fails on them. Update the template handling by either renaming these
sources to a non-JSONC template extension such as .jsonc.tmpl or excluding the
entire lib/cli/src/crewai_cli/templates/json_crew/** set from JSONC linting.
Apply the same fix consistently to agent.jsonc and the other template JSONC
files in that directory.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 9f985614-827e-4356-9681-2bcf68e9579b
📒 Files selected for processing (13)
lib/cli/src/crewai_cli/create_json_crew.pylib/cli/src/crewai_cli/run_crew.pylib/cli/src/crewai_cli/templates/json_crew/.gitignorelib/cli/src/crewai_cli/templates/json_crew/README.mdlib/cli/src/crewai_cli/templates/json_crew/agent.jsonclib/cli/src/crewai_cli/templates/json_crew/agent_settings.jsonclib/cli/src/crewai_cli/templates/json_crew/crew.jsonclib/cli/src/crewai_cli/templates/json_crew/knowledge/user_preference.txtlib/cli/src/crewai_cli/templates/json_crew/pyproject.tomllib/cli/src/crewai_cli/templates/json_crew/task.jsonclib/cli/src/crewai_cli/utils.pylib/cli/tests/test_create_crew.pylib/cli/tests/test_run_crew.py
✅ Files skipped from review due to trivial changes (3)
- lib/cli/src/crewai_cli/templates/json_crew/knowledge/user_preference.txt
- lib/cli/src/crewai_cli/templates/json_crew/README.md
- lib/cli/src/crewai_cli/templates/json_crew/.gitignore
🚧 Files skipped from review as they are similar to previous changes (4)
- lib/cli/src/crewai_cli/templates/json_crew/pyproject.toml
- lib/cli/tests/test_create_crew.py
- lib/cli/src/crewai_cli/utils.py
- lib/cli/src/crewai_cli/run_crew.py
JSON crews were not using existing CLI templates.
Note
Low Risk
Scaffolding and CLI template plumbing only; behavior is intended to stay the same, with added tests for templates and the JSON runner import path.
Overview
JSON crew project scaffolding no longer builds agent, task, and crew JSONC (plus pyproject.toml, README, .gitignore, and knowledge placeholders) from large inline strings in
create_json_crew.py. Those outputs now come fromtemplates/json_crew/via a sharedrender_templatehelper inutils.py(also used bycopy_template), with{{placeholder}}substitution and no recursive expansion inside replacement values.run_crew.pydeferscrewai_core.projectimports untilread_toml,get_crewai_project_type, andconfigured_project_json_crewrun, so the JSON crew subprocess runner can load the current CLI when the project environment ships an oldercrewai_core. Tests cover on-disk templates, safe template rendering, and that runner path.Reviewed by Cursor Bugbot for commit 2dd335b. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
New Features
Bug Fixes
Chores