samwell Claude commited on
Commit
e7caa1e
·
1 Parent(s): 79ed555

Fix Gradio messages format based on official documentation

Browse files

Issue: Gradio msg.submit automatically adds user message to history
Solution: Only append assistant response, not both user and assistant

Based on Gradio 5 official docs for type="messages":
- msg.submit automatically handles adding user message
- Response function should only append assistant's reply
- Format: {"role": "assistant", "content": bot_message}

Fixes: "Data incompatible with messages format" error

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +3 -9
app.py CHANGED
@@ -188,18 +188,12 @@ with gr.Blocks() as demo:
188
  # Get response and visualization
189
  bot_message, viz_image = chat(message, chat_history)
190
 
191
- # Extract text from message
192
- if isinstance(message, dict):
193
- text = message.get("text", "")
194
- else:
195
- text = message
196
-
197
- # Add to chat history - use messages format for newer Gradio
198
  if chat_history is None:
199
  chat_history = []
200
 
201
- # Use dict format with role/content for messages type chatbot
202
- chat_history.append({"role": "user", "content": text})
203
  chat_history.append({"role": "assistant", "content": bot_message})
204
 
205
  return "", chat_history, viz_image
 
188
  # Get response and visualization
189
  bot_message, viz_image = chat(message, chat_history)
190
 
191
+ # Initialize chat history if None
 
 
 
 
 
 
192
  if chat_history is None:
193
  chat_history = []
194
 
195
+ # Gradio automatically adds the user message when using msg.submit
196
+ # We only need to append the assistant's response in messages format
197
  chat_history.append({"role": "assistant", "content": bot_message})
198
 
199
  return "", chat_history, viz_image