Enrique Cardoza commited on
Commit
a4e67d9
·
1 Parent(s): 8e2a9d7

fix(chat): resolve errors in Claude API integration

Browse files

- Remove extraneous fields from message objects sent to Anthropic API
- Fix history processing to handle Gradio's ChatInterface format correctly
- Add debug logging to troubleshoot message structure
- Simplify message formatting to only include required 'role' and 'content' fields
- Fix 'invalid_request_error' caused by unsupported metadata fields

Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -16,15 +16,24 @@ def respond(message, history):
16
  # Format conversation history for Claude
17
  messages = []
18
 
19
- # Add chat history
20
- for h in history:
21
- messages.append({"role": "user", "content": h[0]})
22
- if h[1]: # Check if there's an assistant response
23
- messages.append({"role": "assistant", "content": h[1]})
 
 
 
 
 
 
 
24
 
25
  # Add the current message
26
  messages.append({"role": "user", "content": message})
27
 
 
 
28
  # Call the Claude API
29
  response = client.messages.create(
30
  model="claude-3-7-sonnet-latest",
 
16
  # Format conversation history for Claude
17
  messages = []
18
 
19
+ # Debug: Print the structure of history
20
+ print(f"History: {history}")
21
+
22
+ # Add chat history - clean up message format to only include role and content
23
+ if history:
24
+ for item in history:
25
+ if isinstance(item, dict) and 'role' in item and 'content' in item:
26
+ # Only include role and content fields that Claude API expects
27
+ messages.append({
28
+ "role": item["role"],
29
+ "content": item["content"]
30
+ })
31
 
32
  # Add the current message
33
  messages.append({"role": "user", "content": message})
34
 
35
+ print(f"Messages to send: {messages}")
36
+
37
  # Call the Claude API
38
  response = client.messages.create(
39
  model="claude-3-7-sonnet-latest",