Skip to content

Conversation

@jamesrochabrun
Copy link
Owner

Summary

This PR adds proper handling for response.done messages in the Realtime API, enabling applications to properly surface critical API errors like quota exhaustion and authentication failures.

Problem

Previously, the Realtime API silently swallowed response.done messages, which contain crucial error information. When API errors occurred (e.g., insufficient_quota, invalid_api_key), they were logged as "Unhandled message type" but never surfaced to consuming applications, making debugging nearly impossible.

Solution

1. New Message Type

  • Added responseDone(status: String, statusDetails: [String: Any]?) case to OpenAIRealtimeMessage enum
  • Captures response completion events with full status details

2. Response.done Handler

  • Implemented dedicated handler in OpenAIRealtimeSession for response.done messages
  • Parses error details from status_details.error structure
  • Logs error code and message for debugging

3. Improved Logging

  • Upgraded unhandled message logs from .debug to .warning for better visibility
  • Added full JSON logging for unhandled messages to aid debugging

Error Format

The handler correctly parses OpenAI's error structure:

{
  "type": "response.done",
  "response": {
    "id": "resp_xxx",
    "status": "failed",
    "status_details": {
      "error": {
        "code": "insufficient_quota",
        "message": "You exceeded your current quota..."
      }
    }
  }
}

Breaking Changes

None - this is a purely additive change.

Testing

Tested with:

  • ✅ Insufficient quota error (proper error surfacing)
  • ✅ Successful responses (no side effects)
  • ✅ Invalid API key error
  • ✅ Normal conversation flows

Impact

Applications using SwiftOpenAI Realtime API can now:

  • Detect and display quota/auth errors to users
  • Handle critical errors gracefully (e.g., disconnect on auth failure)
  • Provide meaningful error messages instead of silent failures

🤖 Generated with Claude Code

jamesrochabrun and others added 2 commits November 16, 2025 23:07
- Add responseDone case to OpenAIRealtimeMessage enum to capture response completion events
- Implement handler for response.done messages in OpenAIRealtimeSession
- Parse and expose error details from status_details.error for API errors (insufficient_quota, invalid_api_key, etc.)
- Improve logging: upgrade unhandled message warnings from debug to warning level
- Add detailed JSON logging for debugging unhandled message types

This change ensures that critical API errors like quota exhaustion and authentication failures are properly surfaced to consuming applications instead of being silently discarded.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@jamesrochabrun jamesrochabrun merged commit fe1bc41 into main Nov 17, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants