Skip to main content

Response Mapping

After sending requests to Google’s Gemini API, Antigravity transforms the responses back into the original protocol format expected by the client.

Architecture

Gemini to OpenAI Conversion

Location: src-tauri/src/proxy/mappers/openai/response.rs

Basic Structure

Gemini Response:
OpenAI Response:

Content Extraction

The mapper processes each part:

Thinking Content Separation

Thinking models output two content streams:
  1. content: Final answer visible to user
  2. reasoning_content: Internal reasoning process
This is extracted using the thought: true flag on parts.

Tool Calls Conversion

Gemini Format:
OpenAI Format:
Note: Arguments are re-serialized to JSON string.

Image Responses

Inline images are converted to markdown:
Search results are appended as markdown:

Finish Reason Mapping

Usage Metadata Mapping

Gemini to Claude Conversion

Location: src-tauri/src/proxy/mappers/claude/streaming.rs (non-streaming uses similar logic)

Message Structure

Claude expects a structured message with content blocks:

Content Block Types

1. Thinking Block
2. Text Block
3. Tool Use Block

Stop Reason Logic

Usage Conversion

Claude format includes cache information:

Context Scaling

For large context windows (>1M tokens), usage can be scaled:
This helps calibrate token estimation for ultra-long contexts.

Multi-Candidate Support

Both protocols support multiple response candidates (OpenAI’s n parameter):
Each candidate becomes a separate choice in the response.

Image Generation Responses

For image models: Gemini:
OpenAI:
Claude:

Signature Caching

Thought signatures are automatically cached for future requests:
This enables:
  • Conversation continuity - Signatures persist across turns
  • Retry recovery - Failed requests can reuse signatures
  • Tool loop support - Function calls maintain signature context

Response Validation

Before returning responses, the mapper validates:
  1. Required fields present (id, object, choices)
  2. At least one choice exists
  3. Valid finish reason for completed responses
  4. Usage data matches actual tokens (when available)

Error Responses

When upstream errors occur: OpenAI Format:
Claude Format:
See Error Handling for retry logic.

Performance Optimizations

Zero-Copy Deserialization

Responses use serde_json::from_str with borrowed strings to avoid allocations:

Lazy Evaluation

Usage metadata is only extracted when needed:

String Pooling

Common strings are reused:

See Also