Skip to content

fix(crew): only report real template variables in fetch_inputs#5900

Open
ATOM00blue wants to merge 1 commit into
crewAIInc:mainfrom
ATOM00blue:fix/fetch-inputs-literal-braces
Open

fix(crew): only report real template variables in fetch_inputs#5900
ATOM00blue wants to merge 1 commit into
crewAIInc:mainfrom
ATOM00blue:fix/fetch-inputs-literal-braces

Conversation

@ATOM00blue
Copy link
Copy Markdown

@ATOM00blue ATOM00blue commented May 22, 2026

Problem

Crew.fetch_inputs() is meant to return the placeholder names a crew expects so callers know what to pass to kickoff(). It scans task descriptions / expected outputs and agent role/goal/backstory with the regex \{(.+?)}, which matches any content between braces.

That regex does not match what actually gets interpolated. interpolate_only() (the function that performs the substitution) only replaces valid identifiers via \{([A-Za-z_][A-Za-z0-9_\-]*)}. As a result, literal braces that are common in prompts — embedded JSON examples, output schemas, etc. — are reported as required inputs even though they are never substituted.

Example:

agent = Agent(
    role="Researcher",
    goal="Research on {topic}.",
    backstory='Reply as JSON like {"name": "value"}.',
)
task = Task(
    description='Analyze {topic} and return {"result": [1, 2, 3]}.',
    expected_output="Summary of {topic}.",
    agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])

crew.fetch_inputs()
# before: {'topic', '"name": "value"', '"result": [1, 2, 3]'}
# after:  {'topic'}

The bogus entries are not real variables, so any value passed for them via kickoff(inputs=...) is silently ignored, and tools that build UIs/prompts from fetch_inputs() surface placeholders that can never be filled.

Fix

Reuse the canonical template-variable pattern that interpolate_only already uses, so input discovery and input substitution share a single source of truth and cannot drift. The pattern is promoted to a public TEMPLATE_VARIABLE_PATTERN in string_utils and imported by crew.py. The now-unused re import in crew.py is removed.

Testing

  • Added test_fetch_inputs_ignores_literal_braces, which fails on main (returns the JSON fragments as inputs) and passes with this change.
  • Existing test_fetch_inputs and the task/crew interpolation tests still pass.
  • ruff check, ruff format --check, and mypy are clean on the changed files.

Developed with AI coding assistance; reviewed and submitted by the author.

Per CONTRIBUTING.md, this PR should carry the llm-generated label — I do not have permission to apply labels as an external contributor, so could a maintainer please add it.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed template variable detection to correctly ignore literal braces in strings (e.g., JSON objects).

Review Change Stack

fetch_inputs used a loose `{(.+?)}` regex that matched any braces,
including literal JSON examples and output schemas embedded in task
descriptions or agent backstories. Those are never substituted by
interpolate_only, so they were reported as required inputs that could
not be filled. Reuse the canonical template-variable pattern shared with
interpolate_only so discovery and substitution stay in sync.
Copilot AI review requested due to automatic review settings May 22, 2026 02:54
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

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: 65c9ac8e-4ddc-4b8f-8bdd-59ee043ed6f9

📥 Commits

Reviewing files that changed from the base of the PR and between 179c20b and 50ebb88.

📒 Files selected for processing (3)
  • lib/crewai/src/crewai/crew.py
  • lib/crewai/src/crewai/utilities/string_utils.py
  • lib/crewai/tests/test_crew.py

📝 Walkthrough

Walkthrough

This PR refactors template variable pattern extraction into a shared constant exported from string_utils, enabling both string interpolation and crew input discovery to use the same placeholder-matching rule. Direct re module usage in crew.py is replaced with centralized pattern usage, and a new test validates that placeholder extraction correctly identifies only valid identifier patterns.

Changes

Template Pattern Extraction Consolidation

Layer / File(s) Summary
Shared template pattern contract
lib/crewai/src/crewai/utilities/string_utils.py
TEMPLATE_VARIABLE_PATTERN is introduced as an exported, named constant replacing the internal _VARIABLE_PATTERN, and interpolate_only() is updated to use it for placeholder matching.
Crew input discovery pattern update
lib/crewai/src/crewai/crew.py
Crew.fetch_inputs() now extracts template variables via the centralized TEMPLATE_VARIABLE_PATTERN instead of local regex compilation. The re module import is removed, and the import from string_utils is expanded to include the pattern constant.
Fetch inputs placeholder validation test
lib/crewai/tests/test_crew.py
New test test_fetch_inputs_ignores_literal_braces() verifies that Crew.fetch_inputs() identifies only valid identifier placeholders while correctly ignoring literal braces and non-identifier patterns.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A pattern shared across the land,
No more duplicates, hand in hand!
From string to crew, one truth they know—
Template variables steal the show! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately summarizes the main change: fixing Crew.fetch_inputs() to report only real template variables, not literal braces.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
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 unit tests (beta)
  • Create PR with unit tests

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Aligns Crew.fetch_inputs() with the actual interpolation semantics used by interpolate_only(), so input discovery reports only real template variables (and ignores literal braces common in JSON/schema examples).

Changes:

  • Introduced a shared TEMPLATE_VARIABLE_PATTERN regex in string_utils (used by interpolate_only).
  • Updated Crew.fetch_inputs() to use TEMPLATE_VARIABLE_PATTERN instead of a broad {...} matcher.
  • Added a regression test ensuring literal braces / non-identifier placeholders are not reported as required inputs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/crewai/tests/test_crew.py Adds a regression test covering literal braces and non-identifier brace content in prompts.
lib/crewai/src/crewai/utilities/string_utils.py Promotes the interpolation regex to TEMPLATE_VARIABLE_PATTERN and reuses it in interpolate_only.
lib/crewai/src/crewai/crew.py Makes fetch_inputs() reuse the canonical template-variable pattern and removes the unused re import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -9,7 +9,9 @@
import unicodedata


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants