How to Connect AI Coding Assistants to Third-Party Models with a Local Format-Conversion Proxy
This article explains why mainstream AI coding assistants cannot talk to third-party model endpoints directly, and shows how to build a lightweight local proxy that translates between the assistant's proprietary protocol and the OpenAI-compatible format. It covers environment setup, the three credentials you need from your model provider, the core conversion logic for messages, tool calls, and streaming events, authentication handling, end-to-end validation, and production hardening tips. By the end, you can route your coding workflow to your own model resources with roughly a hundred lines of code.
1. Background: Why You Need a Format-Conversion Proxy
Most mainstream AI coding assistants are tightly coupled to their own chat protocol. A typical implementation sends model requests using a custom message structure: the request body carries a system prompt, an array of multi-turn messages, tool-call blocks, streaming event types, and other fields whose naming and nesting follow that vendor’s own specification.
In practice, however, developers more often work with model services that are compatible with the OpenAI format—whether a self-hosted open-source model or an inference service from a cloud provider (for example, Baidu AI Cloud’s large-model service platform). These interfaces generally follow the de facto standard of a messages array with a role/content structure and a chat/completions endpoint.
The two protocols differ in message structure, how tool calls are expressed, and the chunk format of streaming output, so pointing a coding assistant straight at a third-party model endpoint will not work. The solution is to run a local “translator” service: it listens on a local port, receives the assistant’s proprietary-protocol requests, converts them to the OpenAI-compatible format, forwards them to the model service, and then converts the responses back. This is what a local format-conversion proxy does.
2. Prerequisites: Setting Up the Basics
Environment requirements
Python 3.10 or newer is recommended for two reasons. First, the script relies heavily on async/await for streaming forwarding, and older versions have incomplete support for async context managers. Second, modern type-annotation syntax (such as list[str] and X | None) requires a recent interpreter. You can install it via the official installer, Homebrew (recommended on macOS), or Anaconda.
Install the coding assistant client itself following its official documentation. On macOS, a common approach is to install the corresponding command-line client through a package manager.
Only three base dependencies are needed: fastapi (to build the local HTTP service), uvicorn (the ASGI server), and httpx (an async HTTP client that makes streaming forwarding easy). Install them with one command:
pip install fastapi uvicorn httpx
Getting the three essentials from your model provider
Before configuring the proxy, confirm three key pieces of information with your model service provider (a cloud inference service or a self-hosted gateway):
- API Key: the authentication credential; make sure it has permission to call the target model and enough quota.
- Endpoint URL: usually a completion endpoint in the form
https://xxx/v1/chat/completions. - Model name: the exact model identifier the provider actually supports; it must match the server-side registered name precisely, including case and version number.
Store these three values in environment variables rather than hard-coding them in the script, so credentials do not leak with the code.
3. Core Implementation: Writing the Format-Conversion Proxy
The proxy’s four responsibilities
The proxy is essentially a stateless reverse-conversion pipeline:
- Listen on a local port and receive the proprietary-protocol requests sent by the coding assistant.
- Parse the request body and map the message structure to the OpenAI-compatible format.
- Forward the request to the third-party model endpoint with the correct authentication headers.
- Convert the OpenAI-format response back into a structure the coding assistant recognizes, forwarding streaming chunks chunk by chunk when necessary.
Key mapping differences
When writing the conversion functions, focus on these differences:
- System prompt: in the proprietary protocol it is usually a standalone top-level field, whereas in the OpenAI format it is a message with
roleset tosystem; merge it to the beginning of themessagesarray. - Tool calls: map the assistant’s tool definitions and call blocks to the OpenAI
tools/tool_callsfields; theroleandcontentwrapping of tool-result messages must be converted accordingly. - Streaming events: the proprietary protocol uses a sequence of typed incremental events, while the OpenAI format uses SSE chunks that begin with
data:; map event types to incremental text in both directions. - Stop reason and usage statistics: the two sides use different enum names, so build a lookup table.
Sample code skeleton
from fastapi import FastAPI, Requestimport httpx, os, jsonapp = FastAPI()API_KEY = os.environ["MODEL_API_KEY"]API_URL = os.environ["MODEL_API_URL"]def claude_to_openai(body: dict) -> dict:# Extract the system prompt, convert messages and tools, map parameters...def openai_to_claude(resp: dict) -> dict:# Reverse-map the message structure and stop reason...@app.post("/v1/messages")async def proxy(request: Request):body = claude_to_openai(await request.json())headers = {"Authorization": f"Bearer {API_KEY}"}async with httpx.AsyncClient(timeout=300) as client:r = await client.post(API_URL, json=body, headers=headers)return openai_to_claude(r.json())
Adapting the authentication method
There are two mainstream authentication header styles: Authorization: Bearer {key} and api-key / x-api-key: {key}. Most OpenAI-compatible services accept the former, while some cloud services require the latter. A safe approach is to add an auth_style switch in the configuration, or to send both headers at once (some gateways ignore extra headers, but you should verify this in practice).
4. Integration and Validation
- Point the coding assistant’s endpoint URL to
http://127.0.0.1:<port>and put any placeholder value in the API Key field (the real key is already configured on the proxy side). - First send a simple non-streaming message to confirm the connection works, then validate streaming output and tool-based code-editing scenarios.
- Common troubleshooting: a mismatched model name returns 404/400; a wrong authentication header format returns 401; a missed message-structure conversion breaks tool calls; a proxy timeout set too short truncates long generations.
5. Stability and Engineering Recommendations
For production use, keep the following in mind: add request logging to the proxy to make format issues easier to diagnose; pass through error codes from the model service and wrap them in a friendly way; in streaming scenarios always flush chunk by chunk to avoid long periods of no output on the client; keep all credentials in environment variables or a secrets-management service; and if you need to share the proxy across multiple devices, deploy it on an internal-network machine and restrict the allowed sources.
Overall, for the cost of about a hundred lines of code, this approach removes the core obstacle of protocol heterogeneity, and it is a general path for developers who want to connect their AI coding workflows to their own model resources.
