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:
felix.zoesch
2025-12-15 12:24:21 +01:00
parent d842bc02ad
commit 267425806d

View File

@@ -25,12 +25,20 @@ async def create_event_endpoint(event: EventCreate, db: Session = Depends(get_db
db_event = create_event(db, event)
# 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 = {
"id": db_event.id,
"session_id": db_event.session_id,
"event_type": db_event.event_type,
"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,
"success": db_event.success,
"description": db_event.description,
@@ -86,12 +94,20 @@ async def list_events(
# Convert to response format
event_responses = []
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 = {
"id": event.id,
"session_id": event.session_id,
"event_type": event.event_type,
"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,
"success": event.success,
"description": event.description,