[BUG] Converter._JSON_PATTERN greedily spans to the last brace, breaking extraction of otherwise-valid JSON followed by trailing text
Description
_JSON_PATTERN in lib/crewai/src/crewai/utilities/converter.py is:
_JSON_PATTERN: Final[re.Pattern[str]] = re.compile(r"({.*})", re.DOTALL).* with re.DOTALL is greedy, so search() matches from the first { in the string to the last } anywhere in the string, not to the end of the first well-formed JSON object. If the LLM's raw output is a clean, valid JSON object followed by any trailing text that itself contains a brace pair (a clarifying note, a schema reference, an example, anything), handle_partial_json grabs the whole span including the trailing text, and json.loads fails on the result even though the actual JSON object was perfectly parseable.
This is used by handle_partial_json and async_handle_partial_json (identical logic in both):
def handle_partial_json(...):
match = _JSON_PATTERN.search(result)
if match:
try:
parsed = json.loads(match.group(), strict=False)
except json.JSONDecodeError:
return convert_with_instructions(...) # discards the already-correct output
...So a response that already contains the right answer gets thrown away and the task falls through to convert_with_instructions, which re-invokes the LLM to "fix" formatting that was never broken. That's an unnecessary extra LLM call at minimum, and if the re-prompt produces a different answer, a silently changed result.
Steps to Reproduce
Ran this directly against the current crewAIInc/crewAI main source for lib/crewai/src/crewai/utilities/converter.py:
import re, json
_JSON_PATTERN = re.compile(r"({.*})", re.DOTALL)
# A realistic LLM response: a clean JSON object, plus a trailing note that
# happens to reference a brace-delimited example.
result = '''{"name": "Alice", "age": 30}
Note: if you need a different format, you could also express it as {"name": "Alice"} alone.'''
match = _JSON_PATTERN.search(result)
print(match.group())
json.loads(match.group(), strict=False)Output:
{"name": "Alice", "age": 30}
Note: if you need a different format, you could also express it as {"name": "Alice"}
json.decoder.JSONDecodeError: Extra data: line 3 column 1 (char 30)The match spans from the first { to the last }, swallowing the trailing sentence, so parsing the perfectly valid leading JSON object fails.
Expected behavior
{"name": "Alice", "age": 30} should be extracted and parsed successfully; the trailing commentary should not affect extraction of the JSON object that already appears in full at the start of the string.
Additional context
This is the mirror-image of #5460 (closed via #5461/#5669), which was about the same regex false-positively matching brace-delimited text that isn't JSON at all (GraphQL schemas). This is a false-negative: genuinely valid JSON gets corrupted by what comes after it, because the regex has no way to know where the real object ends.
A PR that fixed exactly this (#6521, fix(converter): don't let trailing braces corrupt valid JSON extraction) replaced the regex with json.JSONDecoder(strict=False).raw_decode(result, start) at the first {, which parses exactly one JSON value and stops, so trailing braces elsewhere can't corrupt the match. It included a repro and a regression test matching the one above. It went stale and was closed by the bot only because it had no linked issue (crewAI requires first-time contributors to reference an open issue), not because the bug wasn't real - I confirmed _JSON_PATTERN is still the unmodified greedy regex on current main. Opening this issue so the fix has something to attach to.
Operating System
Not OS-specific (pure string/regex logic).
Python Version
3.13
crewAI Version
current main (source read directly, not from an installed release)
Source: crewAIInc/crewAI