Fix tool_input JSON parsing for text prompts
The backend was always trying to parse tool_input as JSON, which failed for UserPromptSubmit events that contain plain text prompts. This caused 500 errors when creating or fetching UserPromptSubmit events. Fixed by adding try-except blocks that gracefully handle both JSON and plain text tool_input values. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,12 +25,20 @@ async def create_event_endpoint(event: EventCreate, db: Session = Depends(get_db
|
|||||||
db_event = create_event(db, event)
|
db_event = create_event(db, event)
|
||||||
|
|
||||||
# Convert event to dict for WebSocket
|
# Convert event to dict for WebSocket
|
||||||
|
# Try to parse tool_input as JSON, but keep as string if it fails
|
||||||
|
tool_input_value = None
|
||||||
|
if db_event.tool_input:
|
||||||
|
try:
|
||||||
|
tool_input_value = json.loads(db_event.tool_input)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
tool_input_value = db_event.tool_input
|
||||||
|
|
||||||
event_dict = {
|
event_dict = {
|
||||||
"id": db_event.id,
|
"id": db_event.id,
|
||||||
"session_id": db_event.session_id,
|
"session_id": db_event.session_id,
|
||||||
"event_type": db_event.event_type,
|
"event_type": db_event.event_type,
|
||||||
"tool_name": db_event.tool_name,
|
"tool_name": db_event.tool_name,
|
||||||
"tool_input": json.loads(db_event.tool_input) if db_event.tool_input else None,
|
"tool_input": tool_input_value,
|
||||||
"tool_output": db_event.tool_output,
|
"tool_output": db_event.tool_output,
|
||||||
"success": db_event.success,
|
"success": db_event.success,
|
||||||
"description": db_event.description,
|
"description": db_event.description,
|
||||||
@@ -86,12 +94,20 @@ async def list_events(
|
|||||||
# Convert to response format
|
# Convert to response format
|
||||||
event_responses = []
|
event_responses = []
|
||||||
for event in events:
|
for event in events:
|
||||||
|
# Try to parse tool_input as JSON, but keep as string if it fails
|
||||||
|
tool_input_value = None
|
||||||
|
if event.tool_input:
|
||||||
|
try:
|
||||||
|
tool_input_value = json.loads(event.tool_input)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
tool_input_value = event.tool_input
|
||||||
|
|
||||||
event_dict = {
|
event_dict = {
|
||||||
"id": event.id,
|
"id": event.id,
|
||||||
"session_id": event.session_id,
|
"session_id": event.session_id,
|
||||||
"event_type": event.event_type,
|
"event_type": event.event_type,
|
||||||
"tool_name": event.tool_name,
|
"tool_name": event.tool_name,
|
||||||
"tool_input": json.loads(event.tool_input) if event.tool_input else None,
|
"tool_input": tool_input_value,
|
||||||
"tool_output": event.tool_output,
|
"tool_output": event.tool_output,
|
||||||
"success": event.success,
|
"success": event.success,
|
||||||
"description": event.description,
|
"description": event.description,
|
||||||
|
|||||||
Reference in New Issue
Block a user