Repository navigation
Description
Description
crewai.agents.parser.parse() can take tens of seconds (or minutes) on a single LLM response when the response contains Action: followed by a long run of whitespace and no Action Input:. This happens when a model degenerates into blank lines or indentation, which is a known failure mode for some models.
The cause is ACTION_INPUT_REGEX in lib/crewai/src/crewai/agents/constants.py:
r"Action\s*\d*\s*:\s*(.*?)\s*Action\s*\d*\s*Input\s*\d*\s*:\s*(.*)" # re.DOTALL\s*, the lazy (.*?) and the following \s* can all match the same whitespace, so when there is no Action Input: the regex engine tries every split. The backtracking is cubic in the length of the whitespace run. parse() runs this regex on every agent step, before it checks for Final Answer:.
ACTION_REGEX and ACTION_INPUT_ONLY_REGEX, which run on the same error path, have a smaller quadratic form of the same problem (\s*\d*\s* and a leading \s*). Each takes about 1–2 s on 20,000 characters.
Steps to Reproduce
import time
from crewai.agents.parser import parse, OutputParserError
for n in (1000, 2000, 3000):
text = "Thought: I should search\nAction:" + "\n" * n
start = time.perf_counter()
try:
parse(text)
except OutputParserError:
pass
print(n, f"{time.perf_counter() - start:.2f}s")Output on main (6749ce3), crewAI 1.15.45, Python 3.12, macOS (Apple Silicon):
1000 0.36s
2000 2.85s
3000 7.00s
4,000 newlines take about 20 s. On the raw regex, "Action:" + " " * 5000 took 108.9 s.
Expected behavior
parse() should fail fast with OutputParserError (missing Action Input:), in time roughly linear in the input length.
Screenshots/Code snippets
A possible fix keeps the same matches: remove the redundant \s* around the lazy group (the parser .strip()s both groups anyway), and write \s*\d*\s* as \s*(?:\d+\s*)?, which matches the same strings but can only match them one way:
ACTION_INPUT_REGEX = re.compile(
r"Action\s*(?:\d+\s*)?:(.*?)Action\s*(?:\d+\s*)?Input\s*(?:\d+\s*)?:\s*(.*)", re.DOTALL
)I compared old and new patterns on 500,000 random inputs built from Action, Input, :, whitespace, digits and JSON fragments. Match/no-match, match start, stripped group 1 and raw group 2 were identical in every case, and 50,000-character worst-case inputs take under 10 ms. I have a branch with this change plus regression tests and can open a PR.
Related, lower priority: crewai/experimental/evaluation/json_parser.py uses ```json\s*([\s\S]*?)\s*```, which has the same shape.
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
1.15.45 (main @ 6749ce3)
crewAI Tools Version
N/A
Evidence
Timings above. Also reproduced with the regression tests on Linux (Docker, Python 3.10).
Possible Solution
See the snippet above. Branch: https://github.com/rajumuke-dev/crewAI/tree/fix/agent-parser-regex-backtracking
Additional context
This issue was drafted with AI assistance (GitHub Copilot). I ran and checked every command and timing shown here myself.
Activity
abhi478jeetur-rgb commentedon Nov 30, 2026
Reproduced this on Linux, crewAI 1.15.44, Python 3.12, using your exact snippet. Timings on my machine were a bit slower than yours but the same shape — 1000 newlines: 0.55s, 2000: 4.06s, 3000: 13.86s — so the cubic blowup is real, not macOS-specific.
The mechanism checks out: in
ACTION_INPUT_REGEX, afterAction:the\s*, the lazy(.*?)and the following\s*can all consume the same whitespace, so with noAction Input:anywhere the engine tries every split of the whitespace run before giving up. And sinceparse()runs this regex before it checks forFinal Answer:, one degenerate model response stalls the whole step.I also tried your suggested rewrite. The 50,000-newline worst case drops to under 1ms, and on equivalence I ran 29 targeted cases (numbered actions, extra spaces/tabs, empty input, nested
Action:inside JSON, missingAction Input:, long trailing whitespace, etc.) plus 3,000 seeded random inputs built from the same tokens — match/no-match, match start, stripped group 1 and raw group 2 were identical to the old pattern in every case. I didn't re-run your 500k-input check, but on everything I tried the rewrite looks equivalent, so it seems safe to land.One extra data point on the quadratic side note:
ACTION_REGEXandACTION_INPUT_ONLY_REGEXare instant on inputs where they match (the lazy group just matches empty), but on the failing shape — e.g.Actionfollowed by 20,000 spaces and no colon — they take ~0.6s and ~2.0s respectively, which matches your ~1–2s figure. Worth fixing in the same PR sinceparse()calls both on the error path (parser.py:116, 120).Also confirmed the
json_parser.py:17pattern (```json\s*([\s\S]*?)\s*```) is still there in the installed version — same shape, though I didn't time it.Your branch + regression tests sound like the right vehicle for this, so I'll leave the PR to you.