Spaces:
Sleeping
Sleeping
Fix AnalyticsTool schema rejected by OpenAI function-calling API
Browse filesChange AnalyticsInput fields from str | Dict[str, Any] to str and parse
JSON inside _run(). OpenAI requires additionalProperties: false on all
object schemas, which Dict[str, Any] cannot satisfy.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- tools/analytics_tool.py +13 -4
tools/analytics_tool.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from crewai.tools import BaseTool
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
from typing import Dict, Any, Type
|
|
@@ -5,9 +6,9 @@ from typing import Dict, Any, Type
|
|
| 5 |
|
| 6 |
# ---------- Input Schema ----------
|
| 7 |
class AnalyticsInput(BaseModel):
|
| 8 |
-
market_data: str
|
| 9 |
-
historical_data: str
|
| 10 |
-
sentiment_data: str
|
| 11 |
|
| 12 |
|
| 13 |
# ---------- Tool ----------
|
|
@@ -20,8 +21,16 @@ class AnalyticsTool(BaseTool):
|
|
| 20 |
)
|
| 21 |
args_schema: Type[BaseModel] = AnalyticsInput
|
| 22 |
|
| 23 |
-
def _run(self, market_data:
|
| 24 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ------------------------------------------------------------
|
| 26 |
# 1) Extract fields from structured tool outputs
|
| 27 |
# ------------------------------------------------------------
|
|
|
|
| 1 |
+
import json
|
| 2 |
from crewai.tools import BaseTool
|
| 3 |
from pydantic import BaseModel, Field
|
| 4 |
from typing import Dict, Any, Type
|
|
|
|
| 6 |
|
| 7 |
# ---------- Input Schema ----------
|
| 8 |
class AnalyticsInput(BaseModel):
|
| 9 |
+
market_data: str = Field(..., description="Structured JSON string from MarketDataTool")
|
| 10 |
+
historical_data: str = Field(..., description="Structured JSON string from HistoricalDataTool")
|
| 11 |
+
sentiment_data: str = Field(..., description="Structured JSON string from SentimentTool")
|
| 12 |
|
| 13 |
|
| 14 |
# ---------- Tool ----------
|
|
|
|
| 21 |
)
|
| 22 |
args_schema: Type[BaseModel] = AnalyticsInput
|
| 23 |
|
| 24 |
+
def _run(self, market_data: str, historical_data: str, sentiment_data: str) -> dict:
|
| 25 |
try:
|
| 26 |
+
# Parse JSON strings into dicts
|
| 27 |
+
if isinstance(market_data, str):
|
| 28 |
+
market_data = json.loads(market_data)
|
| 29 |
+
if isinstance(historical_data, str):
|
| 30 |
+
historical_data = json.loads(historical_data)
|
| 31 |
+
if isinstance(sentiment_data, str):
|
| 32 |
+
sentiment_data = json.loads(sentiment_data)
|
| 33 |
+
|
| 34 |
# ------------------------------------------------------------
|
| 35 |
# 1) Extract fields from structured tool outputs
|
| 36 |
# ------------------------------------------------------------
|