Protocol Translation for Coding Assistants Connecting to Heterogeneous LLMs
This article explains why direct connections between coding assistants and heterogeneous LLM services often fail due to protocol mismatches. It compares modifying client source code, deploying a general-purpose gateway, and running a local lightweight proxy, then walks through request conversion, response reconstruction, and streaming conversion. It also covers deployment configuration and layered troubleshooting.
1. Background: Protocol Inconsistency Is the Root Cause of Direct Connection Failures
Coding assistants usually have a built-in fixed interface protocol. They send code context, task instructions, and tool definitions to model services in an agreed structure, then parse returned values in the same structure. Different model services often differ in the following areas:
- Request structure: some use message arrays, others use input item lists, and system instructions are placed differently.
- Tool calls: tool definition description formats, call identifier naming, and parameter serialization differ.
- Streaming output: incremental event types, order, and end markers are not uniform.
- Reasoning content: some models return a separate thought-process field that clients may not recognize.
- State management: some APIs require the client to maintain history, while others support server-side context storage.
If any one of these does not match, the client will report an error during parsing. Therefore, simply replacing the address and key with a new service usually does not work.
2. Comparison of Available Technical Approaches
Common solutions fall into three categories.
The first is modifying client source code and adding an adaptation layer for the new protocol. This offers the strongest control, but requires maintaining a branch long term. Every client upgrade may break the patch, so it is not suitable for individuals or small teams over the long term.
The second is deploying a general-purpose gateway. A gateway can connect multiple entry points and multiple upstreams at the same time, and can also provide authentication, rate limiting, and usage statistics. It is feature-complete, but deployment, configuration, and troubleshooting costs are high. For users who only want to connect one model, it is heavyweight.
The third is running a lightweight protocol translation proxy locally. The proxy is only responsible for translating client requests into the upstream protocol and translating upstream responses back into a format the client understands. It requires little configuration, has a small impact scope, and can be rolled back by stopping it. It is suitable for single-machine development scenarios.
| Dimension | Modify source code | General-purpose gateway | Local lightweight proxy |
|---|---|---|---|
| Correctness | Depends on adaptation quality | Depends on configuration completeness | Depends on conversion rules |
| Deployment complexity | High | High | Low |
| Reversibility | Low | Medium | High |
| Maintenance cost | Changes with client versions | Requires ongoing operations | Relatively low |
For single-machine, single-model scenarios, the local lightweight proxy has the lowest overall cost.
3. Core Parts of Protocol Translation
3.1 Request Conversion
Request conversion needs to accomplish three things: expand input items, map system instructions, and convert tool definitions. Input items may include ordinary messages, tool execution results, and other types. Each item needs to be evaluated and converted to the upstream message format. The following example is only for illustrating the idea; field names should follow the actual protocol documentation.
def convert_request(client_req: dict, model_map: dict) -> dict:messages = []if client_req.get("system_prompt"):messages.append({"role": "system", "content": client_req["system_prompt"]})for item in client_req.get("items", []):kind = item.get("kind")if kind == "text":messages.append({"role": item["role"], "content": item["text"]})elif kind == "tool_result":messages.append({"role": "tool","tool_call_id": item["call_id"],"content": item["output"],})else:raise ValueError(f"Unsupported input type: {kind}")return {"model": model_map.get(client_req["model"], client_req["model"]),"messages": messages,"stream": client_req.get("stream", False),}
For unknown types, raise an explicit error rather than silently dropping them. Silent dropping causes missing context, and the problem is hard to notice.
3.2 Response Reconstruction
For non-streaming responses, the upstream generation result, usage information, and tool calls need to be reassembled into the structure expected by the client. Tool call parameters are usually JSON in string form. During conversion, keep them as-is; do not deserialize and reserialize in the middle, otherwise field order or numeric precision may change. Map usage fields by name. When a field is missing, fill in zero and log it for later verification.
3.3 Streaming Conversion
Streaming output is the most error-prone part of conversion. The upstream returns text, tool parameters, and reasoning content as incremental chunks. The proxy needs to parse them chunk by chunk and generate the event sequence required by the client. Follow three principles: preserve event order, forward chunk by chunk without buffering the whole response, and emit a completion marker at the end.
def stream_convert(upstream_chunks):yield {"event": "start"}for chunk in upstream_chunks:delta = chunk.get("delta", {})if delta.get("reasoning"):yield {"event": "reasoning_delta", "text": delta["reasoning"]}if delta.get("content"):yield {"event": "text_delta", "text": delta["content"]}for call in delta.get("tool_calls", []):yield {"event": "tool_call_delta", "data": call}yield {"event": "done"}
Reasoning content should be labeled separately from the final answer. If they are mixed together, the client may show the thought process to users as the answer.
4. Deployment and Configuration Essentials
- Version check: Confirm that the proxy tool meets the minimum version requirement. Older versions may lack protocol translation capabilities. Back up the current configuration file before upgrading.
- Listen address: Bind the proxy only to the local loopback address, not to a public network interface, to prevent others from consuming key quota through it.
- Key management: Store upstream keys through environment variables or the system keychain. Do not write them into code repositories, scripts, or logs.
- Model mapping: Configure the mapping from client model names to upstream model names in the proxy, and enable the corresponding route mapping option.
- Client endpoint: Change the client’s API address to the local proxy address. The local key can be a placeholder value, and the proxy replaces it with the real key.
5. Common Troubleshooting
Troubleshoot layer by layer along the chain for the most efficient localization: bypass the proxy and call the upstream API directly with an HTTP request tool to confirm that the key, network, and model name are correct.
