Agent fails after Composio tool execution despite valid cleaned response #2408
Unanswered
mayurjadhav2002
asked this question in
providers
Replies: 1 comment
-
|
Agent failing after tool execution with valid response is usually a parsing issue. Here is how to debug and fix: Common Causes
Debugging Patternimport json
from typing import Any
class ToolResponseDebugger:
@staticmethod
def validate_response(response: Any, expected_schema: dict = None) -> dict:
issues = []
# Check type
if isinstance(response, str):
try:
parsed = json.loads(response)
issues.append("Response is JSON string, might need parsing")
except:
pass
# Check size
response_str = str(response)
if len(response_str) > 10000:
issues.append(f"Response large: {len(response_str)} chars")
# Check encoding
try:
response_str.encode("utf-8")
except UnicodeError:
issues.append("Encoding issues detected")
# Check schema
if expected_schema and isinstance(response, dict):
for key in expected_schema.get("required", []):
if key not in response:
issues.append(f"Missing required field: {key}")
return {
"valid": len(issues) == 0,
"issues": issues,
"response_type": type(response).__name__,
"response_size": len(response_str)
}Fix: Response Normalizerdef normalize_tool_response(response: Any) -> str:
"""Ensure consistent response format for agents"""
# Handle None
if response is None:
return json.dumps({"status": "success", "result": None})
# Handle dict
if isinstance(response, dict):
# Truncate large values
cleaned = {}
for k, v in response.items():
v_str = str(v)
if len(v_str) > 5000:
cleaned[k] = v_str[:5000] + "... (truncated)"
else:
cleaned[k] = v
return json.dumps(cleaned, ensure_ascii=True, default=str)
# Handle string
if isinstance(response, str):
if len(response) > 10000:
return response[:10000] + "... (truncated)"
return response
# Fallback
return json.dumps({"result": str(response)})
# Wrap Composio tool calls
async def safe_tool_call(tool, args):
try:
result = await tool.execute(**args)
return normalize_tool_response(result)
except Exception as e:
return json.dumps({"error": str(e), "tool": tool.name})Likely Fix for Your CaseCheck if the response needs explicit JSON serialization before returning to the agent. More on tool patterns: https://github.com/KeepALifeUS/autonomous-agents |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When using a Composio Gmail tool with schema_modifier + after_execute sanitization, the tool executes successfully and returns a cleaned response, but the agent fails while generating the final reply, resulting in a generic error:
“Sorry — something went wrong while processing your request.”
This happens after tool execution, not during the tool call itself.
Basically GMAIL is fetching top 5 emails but with their complete html body including base64 which leads to input limit error.
How can fix this, any advice?
Environment
• Python
• Agents framework (Agent, Runner)
• Composio Gmail tools
• schema_modifier + after_execute
• Redis used for toolkit caching
• Model: gpt-4o-mini
Beta Was this translation helpful? Give feedback.
All reactions