JSON Parsing Errors
Problem: Unable to parse agent responses as JSON Solution: Request specific JSON format and implement robust parsingRequest Clean JSON
Always specify that you want raw JSON without markdown:- JavaScript
- Python
- HTTPie
await client.sendMessage(sessionId, `
Extract product information.
Return ONLY valid JSON (no markdown, no explanations):
{
"products": [
{
"name": "Product Name",
"price": 99.99
}
]
}
Do not wrap in markdown code blocks.
Do not include any text before or after the JSON.
`);
send_message(session_id, """
Extract product information.
Return ONLY valid JSON (no markdown, no explanations):
{
"products": [
{
"name": "Product Name",
"price": 99.99
}
]
}
Do not wrap in markdown code blocks.
Do not include any text before or after the JSON.
""")
http POST https://api.agi.tech/v1/sessions/$SESSION_ID/message \
Authorization:"Bearer $AGI_API_KEY" \
message="Extract product information. Return ONLY valid JSON (no markdown, no explanations). Do not wrap in markdown code blocks."
Robust JSON Parsing
Handle cases where agent wraps JSON in markdown:- JavaScript
- Python
- HTTPie
function parseAgentJson(response) {
try {
// Try direct parse
return JSON.parse(response);
} catch (e) {
// Try extracting JSON from markdown
const jsonMatch = response.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
return JSON.parse(jsonMatch[1]);
}
// Try finding JSON object
const jsonObjectMatch = response.match(/\{[\s\S]*\}/);
if (jsonObjectMatch) {
return JSON.parse(jsonObjectMatch[0]);
}
throw new Error('No valid JSON found in response');
}
}
// Usage
const result = await session.runTask('Extract product info as JSON');
const data = parseAgentJson(result);
import json
import re
def parse_agent_json(response):
"""Safely parse JSON from agent"""
try:
# Try direct parse
return json.loads(response)
except json.JSONDecodeError:
# Try extracting JSON from markdown
json_match = re.search(r'```json\n(.*?)\n```', response, re.DOTALL)
if json_match:
return json.loads(json_match.group(1))
# Try finding JSON object
json_match = re.search(r'\{.*\}', response, re.DOTALL)
if json_match:
return json.loads(json_match.group(0))
raise ValueError("No valid JSON found in response")
# Usage
result = session.run_task('Extract product info as JSON')
data = parse_agent_json(result)
# Send message requesting JSON
RESPONSE=$(http POST https://api.agi.tech/v1/sessions/$SESSION_ID/message \
Authorization:"Bearer $AGI_API_KEY" \
message="Extract product information. Return ONLY valid JSON (no markdown, no explanations). Do not wrap in markdown code blocks.")
# Parse JSON (jq handles this automatically)
echo "$RESPONSE" | jq '.products[]'
# Or extract JSON from markdown if needed
echo "$RESPONSE" | sed -n '/```json/,/```/p' | sed '1d;$d' | jq .
Always specify the exact JSON structure you expect in your instructions. This helps the agent return properly formatted data.
Related Guides
Task Design
Learn how to write effective task instructions
Troubleshooting Home
Back to troubleshooting overview