fix: handle infinity values in JSON serialization for API responses
- Add sanitize_json_data() function to convert infinity/NaN to JSON-compliant strings
- Fix /execute_js endpoint returning ValueError: Out of range float values are not JSON compliant: inf
- Fix /crawl endpoint batch responses with infinity values
- Fix /crawl/stream endpoint streaming responses with infinity values
- Fix /crawl/job endpoint background job responses with infinity values
The sanitize_json_data() function recursively processes response data:
- float('inf') → \"Infinity\"
- float('-inf') → \"-Infinity\"
- float('nan') → \"NaN\"
This prevents JSON serialization errors when JavaScript execution or crawling operations produce infinity values, ensuring all API endpoints return valid JSON.
Fixes: API endpoints crashing with infinity JSON serialization errors
Affects: /execute_js, /crawl, /crawl/stream, /crawl/job endpoints
This commit is contained in:
@@ -331,6 +331,27 @@ async def generate_pdf(
|
||||
return {"success": True, "pdf": base64.b64encode(pdf_data).decode()}
|
||||
|
||||
|
||||
def sanitize_json_data(data):
|
||||
"""
|
||||
Recursively sanitize data to handle infinity and NaN values that are not JSON compliant.
|
||||
"""
|
||||
import math
|
||||
|
||||
if isinstance(data, dict):
|
||||
return {k: sanitize_json_data(v) for k, v in data.items()}
|
||||
elif isinstance(data, list):
|
||||
return [sanitize_json_data(item) for item in data]
|
||||
elif isinstance(data, float):
|
||||
if math.isinf(data):
|
||||
return "Infinity" if data > 0 else "-Infinity"
|
||||
elif math.isnan(data):
|
||||
return "NaN"
|
||||
else:
|
||||
return data
|
||||
else:
|
||||
return data
|
||||
|
||||
|
||||
@app.post("/execute_js")
|
||||
@limiter.limit(config["rate_limiting"]["default_limit"])
|
||||
@mcp_tool("execute_js")
|
||||
@@ -389,7 +410,9 @@ async def execute_js(
|
||||
results = await crawler.arun(url=body.url, config=cfg)
|
||||
# Return JSON-serializable dict of the first CrawlResult
|
||||
data = results[0].model_dump()
|
||||
return JSONResponse(data)
|
||||
# Sanitize data to handle infinity values
|
||||
sanitized_data = sanitize_json_data(data)
|
||||
return JSONResponse(sanitized_data)
|
||||
|
||||
|
||||
@app.get("/llm/{url:path}")
|
||||
|
||||
Reference in New Issue
Block a user