Skip to main content

Overview

This example demonstrates how to use the CLI Proxy API translator package to transform requests and responses between different AI provider formats. The translator system enables clients using one API format (e.g., OpenAI) to communicate with providers that use different formats (e.g., Gemini, Claude, etc.).

Use Case

Use the translator package when you need to:
  • Convert OpenAI-formatted requests to provider-specific formats
  • Transform provider responses back to OpenAI format for clients
  • Support multiple providers with a unified client interface
  • Test format transformations during development
  • Build custom translators for new providers

Complete Source Code

Key Concepts

1. Built-in Translators

The SDK includes built-in translators for popular AI providers. Import the builtin package to access them:
This blank import registers translators for:
  • OpenAI
  • Anthropic (Claude)
  • Google (Gemini)
  • And other supported providers

2. Format Constants

The translator package provides format constants for common providers:
  • translator.FormatOpenAI - OpenAI chat completion format
  • translator.FormatGemini - Google Gemini format
  • translator.FormatClaude - Anthropic Claude format
  • And others

3. Checking Translator Availability

Before translating, you can check if a translator exists for a format pair:

4. Request Translation

Transform a request from one format to another:

5. Response Translation

Transform a provider’s response back to the client’s expected format:

Non-Streaming Responses

Streaming Responses

For streaming responses, use TranslateStreamByFormatName which returns a channel of translated chunks.

How to Run

  1. Run the example:
  1. The example will:
    • Check if a Gemini-to-OpenAI translator exists
    • Translate an OpenAI request to Gemini format
    • Transform a Gemini response back to OpenAI format
    • Print the results

Example Flow

This example demonstrates a complete translation flow:

Step 1: Original OpenAI Request

Step 2: Translated to Gemini Format

The translator converts the OpenAI format to Gemini’s native format, adjusting field names, structure, and conventions.

Step 3: Gemini Response

The provider returns a response in Gemini format with features like thoughts and function calls:

Step 4: Converted to OpenAI Format

The translator converts the response back to OpenAI format that clients expect:

Supported Translations

The built-in translators support bidirectional translation between:
  • OpenAI ↔ Gemini
  • OpenAI ↔ Claude
  • OpenAI ↔ Cohere
  • OpenAI ↔ Mistral
  • And other provider combinations

Custom Translators

You can register custom translators for new providers or custom formats. See the Custom Provider Example for how to register translators using translator.Register().

Basic Registration

Advanced Features

Context-Aware Translation

The translator can access both the original request and translated request during response translation, allowing for context-aware transformations:

Parameter Passing

The optional param parameter allows passing additional data through the translation pipeline, useful for:
  • Custom metadata
  • Feature flags
  • Provider-specific options

Error Handling

If a translator doesn’t exist for a format pair:
  • TranslateRequestByFormatName returns the original request unchanged
  • TranslateNonStreamByFormatName returns the raw response unchanged
  • Use HasResponseTransformerByFormatName to check availability before translating