Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task.py: try to find json in task output using regex #491

Merged
merged 2 commits into from May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/crewai/task.py
@@ -1,3 +1,4 @@
import re
import threading
import uuid
from typing import Any, Dict, List, Optional, Type
Expand Down Expand Up @@ -245,7 +246,16 @@ def _export_output(self, result: str) -> Any:
return exported_result.model_dump()
return exported_result
except Exception:
pass
# sometimes the response contains valid JSON in the middle of text
match = re.search(r"({.*})", result, re.DOTALL)
if match:
try:
exported_result = model.model_validate_json(match.group(0))
if self.output_json:
return exported_result.model_dump()
return exported_result
except Exception:
pass

llm = self.agent.function_calling_llm or self.agent.llm

Expand Down