copilot-cli icon indicating copy to clipboard operation
copilot-cli copied to clipboard

Can´t use DeepSeek API

Open raffaeler opened this issue 7 months ago • 14 comments

Describe the bug

When I run the following command:

export COPILOT_PROVIDER_BASE_URL="https://api.deepseek.com" \
export COPILOT_PROVIDER_API_KEY="...." \
export COPILOT_PROVIDER_TYPE="openai" \
export COPILOT_MODEL="deepseek-v4-pro" \
copilot

When I make a question to the copilot, I get this answer back:

● Request failed (transient_bad_request). Retrying...
● Request failed (transient_bad_request). Retrying...
✗ 400 The `reasoning_content` in the thinking mode must be passed back to the API. 

I confirm that the DeepSeek API needs the entire reasoning_content field to be returned back to the API every time. In my own code I had the same problem and could rapidly fix the issue by sending back the the entire conversation.

Affected version

No response

Steps to reproduce the behavior

I run the following command:

export COPILOT_PROVIDER_BASE_URL="https://api.deepseek.com" \
export COPILOT_PROVIDER_API_KEY="...." \
export COPILOT_PROVIDER_TYPE="openai" \
export COPILOT_MODEL="deepseek-v4-pro" \
copilot

Ensure to have a valid api key for the DeepSeek endpoint

Expected behavior

I expect the request to be fulfilled.

Additional context

No response

raffaeler avatar Apr 27 '26 09:04 raffaeler

I created this Proxy as a workaround for this bug: https://github.com/raffaeler/mindthegap

If you like it, please give it a star.

raffaeler avatar Apr 29 '26 09:04 raffaeler

SxS team evaluating DeepSeek is also hitting the same issue https://github.com/github/cli-side-by-side-launcher

abchatra avatar Apr 30 '26 17:04 abchatra

Using DeepSeek's Anthropic-compatible API can resolve this issue, but it may still be triggered under certain circumstances. For example, during a long conversation, if you run commands like copilot CLI or /plugins to check some information, then wait for a while before continuing the conversation, the issue might occur. Additionally, the DeepSeek API included in the OpenCode Go subscription plan also has usability problems, as it uses an OpenAI-compatible API.

Aquaakuma avatar May 03 '26 01:05 Aquaakuma

@Aquaakuma I tried but unfortunately it does not resolve. I could fully resolve using my own proxy (see my comment above) which also has the bonus to show the thinking tokens.

I would expect the Copilot CLI to give a better user experience in adding, editing and switching providers/models. At the moment I am using a tool called cpm.

raffaeler avatar May 03 '26 07:05 raffaeler

@Aquaakuma I tried but unfortunately it does not resolve. I could fully resolve using my own proxy (see my comment above) which also has the bonus to show the thinking tokens.

I would expect the Copilot CLI to give a better user experience in adding, editing and switching providers/models. At the moment I am using a tool called cpm.

If you are using the DeepSeek Official API, you can try this method: [接入 GitHub Copilot CLI](https://api-docs.deepseek.com/zh-cn/quick_start/agent_integrations/copilot_cli). but it may still be triggered in some times.

Aquaakuma avatar May 03 '26 07:05 Aquaakuma

If you are using the DeepSeek Official API, you can try this method: [接入 GitHub Copilot CLI](https://api-docs.deepseek.com/zh-cn/quick_start/agent_integrations/copilot_cli). but it may still be triggered in some times.

@Aquaakuma No, it does not help as I wrote in my previous comment. I tried the anthropic mode but GitHub Copilot CLI still complains about the reasoning_content field.

The DeepSeek API expects the reasoning_content field is always returned to the API after the first turn. My proxy moves the reasoning_content into the main content and then puts it back in the reasoning_content at the following turn. This works with any client, not just with the CLI.

raffaeler avatar May 03 '26 08:05 raffaeler

If you are using the DeepSeek Official API, you can try this method: [接入 GitHub Copilot CLI](https://api-docs.deepseek.com/zh-cn/quick_start/agent_integrations/copilot_cli). but it may still be triggered in some times.

@Aquaakuma No, it does not help as I wrote in my previous comment. I tried the anthropic mode but GitHub Copilot CLI still complains about the reasoning_content field.

The DeepSeek API expects the reasoning_content field is always returned to the API after the first turn. My proxy moves the reasoning_content into the main content and then puts it back in the reasoning_content at the following turn. This works with any client, not just with the CLI.

This implementation approach is not very user-friendly. The ideal solution is to fix it in the Copilot CLI source code, or perhaps to execute scripts during the conversation via hooks, ensuring that it is invoked for every conversation.

Aquaakuma avatar May 03 '26 11:05 Aquaakuma

This implementation approach is not very user-friendly. The ideal solution is to fix it in the Copilot CLI source code, or perhaps to execute scripts during the conversation via hooks, ensuring that it is invoked for every conversation.

@Aquaakuma I opened this issue to solicit the change in GitHub Copilot CLI, but the same problem also affects Visual Studio Code and Visual Studio 2026. This means that currently the proxy is the best workaround. I am not sure the hooks can get to the reasoning_content. Most probably they do not have access to it. Give a try ;-)

raffaeler avatar May 03 '26 12:05 raffaeler

This appears to be resolved as of GitHub Copilot CLI v1.0.88. Please update to that release or a newer stable version. We're closing this as completed. If it still happens, please comment with your version and reproduction steps.

copilot-cli-bot[bot] avatar Sep 23 '26 10:09 copilot-cli-bot[bot]

Just to be clear: this was not fixed in Github CLI. But it is working now thanks to Deepseek who implemented the Open Responses protocol. Kudos to them.

raffaeler avatar Sep 24 '26 08:09 raffaeler

I asked copilot to fix itself and document the change. Here's the output it generated:

Copilot CLI — reasoning_content round-trip fix

Date: 2026-09-26 Copilot CLI: 1.0.88 (/usr/local/lib/node_modules/@github/copilot, git commit 52f75603) Provider: BYOK OpenAI-compatible → https://api.deepseek.com/v1, model deepseek-flash


TL;DR

Copilot intermittently failed every turn with:

400 The `reasoning_content` in the thinking mode must be passed back to the API.

Cause: DeepSeek's thinking mode requires every replayed assistant message that carries tool_calls to also carry a reasoning_content string, and Copilot's native runtime sometimes omits it.

Fix: a small local proxy (~/.copilot-reasoning-fix/reasoning-proxy.py) is inserted in front of the provider endpoint and guarantees the field is present on every request. The copilot launcher was patched to start the proxy automatically and to read ~/.copilot_config, which now also holds a COPILOT_SHOW_REASONING switch that turns reasoning display on/off by environment variable.

Nothing needs to be run by hand — just start copilot as usual.


1. The problem

~/.copilot/settings.json and ~/.copilot_config point Copilot at DeepSeek (COPILOT_PROVIDER_BASE_URL=https://api.deepseek.com/v1, COPILOT_MODEL=deepseek-flash). The provider runs in thinking mode, where the model emits a reasoning_content field alongside its answer and tool calls.

Copilot's own session history shows the failure was frequent and long-standing:

Metric Value
session.error events with this message 93 (92 × reasoning_content, 1 × Anthropic-style content[].thinking)
Distinct sessions affected 14
First / last occurrence 2026-08-22 / 2026-09-26

Because the error is raised on the request, the turn dies before the model ever runs — so the session appears to stall, then continues only when the user sends a new message.


2. Root cause

2.1 What the API actually requires (measured, not guessed)

Probed directly against https://api.deepseek.com/v1/chat/completions:

Assistant message with tool_calls Result
no reasoning_content key, tool-call id unknown to the server HTTP 400 — the reported error
"reasoning_content": null HTTP 400
"reasoning_content": "" HTTP 200
"reasoning_content": "<real text>" HTTP 200

So the rule is: the key must be present and must be a string. Empty is acceptable; absent or null is not.

2.2 What Copilot sends (measured)

Running a real session through a logging proxy showed Copilot re-sending:

{
  "role": "assistant",
  "content": "I'll run both commands.",
  "tool_calls": [ { "id": "call_00_...", "type": "function", "function": { ... } } ]
}

— no reasoning_content, even though the model had streamed one.

2.3 When it happens

Cross-referencing all 93 failures with the assistant message that preceded them gives a single, unambiguous trigger:

Preceding assistant tool call tool-call id reasoning stored failures
read_bash locally generated UUID none 93 / 93

Every recorded failure follows the same sequence:

  1. A background shell is started and the model ends its turn.
  2. The shell finishes, so the CLI injects a system.notification and manufactures an assistant message containing a read_bash tool call to collect the output. That message is created outside the model response, so it never carries reasoning — its session event has no reasoningText, reasoningWireField or reasoningBlocks.
  3. The next request replays that message. Its tool-call id is a locally generated UUID the API has never issued, so the API cannot fill in the reasoning itself and rejects the request.

Reproduction that fails 100 % of the time on the unpatched install:

copilot -p "Start a background shell with the bash tool (async mode) that runs:
sleep 25 && echo late-done. Then end your turn immediately WITHOUT reading its output.
Just say 'started'." --allow-all-tools

Before the fix this produced:

400 The `reasoning_content` in the thinking mode must be passed back to the API. (request_id: 53ff045e-39f7-41e0-92f1-d53faf6518b5)

The reasoningText / reasoningWireField / reasoningBlocks fields in schemas/session-events.schema.json show the runtime intends to round-trip reasoning per dialect — it just misses this path.

2.4 Why there is no one-line fix in the request builder

This was investigated carefully, because a one-line change would obviously be preferable:

  • github/copilot-cli is closed source — the repository contains only README.md, LICENSE.md, changelog.md, install.sh and .github/. There is no source to download or build from.
  • The installed CLI is a Node single-executable-application: a 169 MB ELF with an embedded sea-loader.js plus a copilot.tgz asset that is unpacked to ~/.cache/copilot/pkg/linux-x64/1.0.88/ (app.js, index.js, sdk/, …).
  • The request object is not built in any of those JavaScript files. app.js, index.js, sdk/index.js and copilot-sdk/index.js contain no chat/completions path, no baseURL, no stainless and no provider transport code — app.js only holds the help text for the COPILOT_PROVIDER_* variables.
  • It is built inside prebuilds/linux-x64/runtime.node: a 101,936,656-byte stripped Rust shared object (reqwest/hyper). The HTTP header names the runtime emits (X-Stainless-Package-Version, X-Stainless-Timeout, …) exist there as Rust string literals, and reasoningWireField (the round-trip machinery) lives there too.
  • A NODE_OPTIONS=--require … probe confirmed no JavaScript layer touches the outgoing model request, and strace -f showed the socket is opened by that native process.

Patching serialisation logic inside a stripped 100 MB Rust binary is not something that can be done reliably, so the fix was placed at the only layer we control: the wire.


3. The fix

Four components. All are installed; nothing needs to be started manually.

3.1 ~/.copilot-reasoning-fix/reasoning-proxy.py (new, 20,759 bytes)

A transparent reverse proxy for the OpenAI-compatible endpoint. It relays request and response bodies byte-for-byte, with two additions:

  1. Capture (upstream → Copilot): parses the streamed reasoning_content and the streamed tool_calls, and remembers the reasoning keyed by tool-call id (and by a tool-signature fingerprint as a fallback). State is persisted to ~/.copilot-reasoning-fix/state.json, so it survives restarts and resumed sessions.
  2. Inject (Copilot → upstream): every assistant message carrying tool_calls is guaranteed a reasoning_content string — the remembered text when known, otherwise "". Messages that already have a non-empty value are untouched.

Design notes:

  • Accept-Encoding is forced to identity so responses always stay parseable and are relayed verbatim.
  • Everything else (path, query, Authorization, provider-specific headers) is relayed untouched; the proxy never stores or logs credentials.
  • GET /__reasoning_proxy/health returns JSON identifying the instance ({"proxy":"reasoning-proxy","pid":…,"upstream":…,"fallback":…}) so the launcher can detect a live instance and confirm it is pointed at the provider currently in use.
  • --dump DIR writes raw/rewritten requests and responses for diagnostics.

Run standalone if ever needed:

python3 ~/.copilot-reasoning-fix/reasoning-proxy.py \
  --listen 127.0.0.1:8899 --upstream https://api.deepseek.com/v1 \
  --state ~/.copilot-reasoning-fix/state.json

3.2 /usr/local/lib/node_modules/@github/copilot/npm-loader.js (patched)

The copilot launcher. Original was a 1,185-byte minified spawner; it is now 9,294 bytes of readable code that additionally:

  1. Reads ~/.copilot_config (export KEY=value / KEY=value lines) and applies it as environment defaults, so the provider configuration and the switches below work however Copilot is launched — interactive shell, app, editor, desktop launcher. Real environment variables always win over the file.
  2. When a BYOK base URL is configured, ensures the proxy is running (health-checked on 127.0.0.1:8899, started detached with python3 if not) and rewrites COPILOT_PROVIDER_BASE_URL to http://127.0.0.1:8899/v1, keeping the original as the proxy's upstream.
  3. If a proxy is already listening but reports a different upstream (e.g. left over from a previous provider selection), it stops that instance and starts a correct one — so switching providers in ~/.copilot_config never silently routes to the old endpoint.
  4. If the proxy cannot be started it prints a warning and continues with the original base URL, so Copilot still works (just with the original bug).
  5. Spawns the native binary with the computed environment.

The original handleSpawnResult export is preserved.

3.3 ~/.cache/copilot/pkg/linux-x64/1.0.88/app.js (patched)

Adds one function and one line to the settings normaliser Ije() — the single point where loaded settings become the object the TUI uses:

function __copilotShowReasoningOverride(){
  let _v=process.env.COPILOT_SHOW_REASONING;
  if(_v===void 0)return;
  let _t=_v.trim().toLowerCase();
  if(_t==="")return;
  return ["1","true","yes","on","show","always","enabled"].includes(_t)
}
function Ije(e,t){
  let n=e.banner??(t?"once":"never"),_o=__copilotShowReasoningOverride();
  return {...e, banner:…, …, ...(_o===void 0?{}:{showReasoning:_o})}
}
  • 7,856,629 → 7,856,928 bytes; the change is one contiguous region (verified by prefix/suffix diff).
  • undefined means "leave settings.json in charge"; an unset or empty COPILOT_SHOW_REASONING is therefore a no-op.
  • The value flows through the same path the UI's own reasoning toggle uses (xn.showReasoning → native modelCliReasoningSummariesPolicy), verified live: COPILOT_SHOW_REASONING=true → persistedShowReasoning:true, false → persistedShowReasoning:false.

3.4 Configuration files

~/.copilot/settings.json — reasoning display turned on (same key the UI writes when you toggle it):

{ "showReasoning": true, "skillDirectories": [ … ] }

~/.copilot_config — appended a documented block:

export COPILOT_SHOW_REASONING=true
export COPILOT_REASONING_PROXY=on
# export COPILOT_REASONING_PROXY_LISTEN=127.0.0.1:8899
# export COPILOT_REASONING_PROXY_FALLBACK=on
# export COPILOT_REASONING_PROXY_PYTHON=python3

4. Configuration reference

Variable Values Default Effect
COPILOT_REASONING_PROXY on/off on (whenever a BYOK base URL exists) Route provider traffic through the round-trip proxy
COPILOT_REASONING_PROXY_LISTEN host:port 127.0.0.1:8899 Proxy listen address
COPILOT_REASONING_PROXY_FALLBACK on/off on Inject reasoning_content="" when the real reasoning was never captured (legacy/resumed history). off = only replay captured reasoning
COPILOT_REASONING_PROXY_PYTHON path python3 Interpreter used to start the proxy
COPILOT_SHOW_REASONING on/off unset → settings.json wins Override the reasoning display setting

All of them are read from ~/.copilot_config by the patched launcher, and a real environment variable of the same name takes precedence.


5. Verification

Everything below was executed on this machine.

a. Reproduce the bug on the pre-fix install — 100 % reproducible:

copilot -p "Start a background shell … sleep 25 && echo late-done … Just say 'started'." --allow-all-tools
# → 400 The `reasoning_content` in the thinking mode must be passed back to the API.

b. Same command with the fix (with no provider variables in the shell, proving the launcher supplies them from ~/.copilot_config and starts the proxy itself):

● Read shell output  └ 2 lines…     late-done
Tokens ↑ 34.0k … ↓ 148 (42 reasoning)

~/.copilot-reasoning-fix/proxy.log: started proxy http://127.0.0.1:8899/v1 -> https://api.deepseek.com/v1

(On subsequent launches the same line reads reusing proxy ….)

c. Proxy restart — killed the proxy, re-ran the same command: it was started again (new pid) and the turn succeeded.

c2. Stale proxy replacement — started a proxy on 127.0.0.1:8899 pointing at https://openrouter.ai/api/v1, then ran Copilot. The launcher detected the mismatch:

stopping stale proxy pid=2202164 (upstream https://openrouter.ai/api/v1)
started proxy http://127.0.0.1:8899/v1 -> https://api.deepseek.com/v1

and health then reported the correct upstream on the new pid.

d. Resume of a session that had failed before the fix (ae7a65c3…): resumed cleanly and the model correctly recalled the earlier background output — this exercises the reasoning_content="" fallback for history recorded before the fix.

e. Injection is real, not theoretical — verbose proxy run over a live session:

-> POST /v1/chat/completions (reasoning_content injected on 1 message(s))
"POST /v1/chat/completions HTTP/1.1" 200 -

and the captured pre-fix request body confirms Copilot's assistant tool message had no reasoning_content.

f. Off switch — COPILOT_REASONING_PROXY=off copilot -p "…" talks straight to DeepSeek again (proxy disabled by COPILOT_REASONING_PROXY in the log).

g. Display switch — verified live in the TUI with temporary instrumentation that the env value reaches the native policy (persistedShowReasoning:true / :false). Instrumentation was removed afterwards (verified: 0 probe strings remain).


6. Operating it

Thing Location
Proxy state (captured reasoning) ~/.copilot-reasoning-fix/state.json
Launcher decisions log ~/.copilot-reasoning-fix/proxy.log
Proxy script ~/.copilot-reasoning-fix/reasoning-proxy.py
Backups of every original file ~/.copilot-reasoning-fix/*.orig
  • The proxy is started on demand and is shared by all Copilot processes. It can be killed at any time — the next copilot invocation restarts it.
  • Diagnostics: run a copy with --dump /tmp/proxy-dump and inspect the raw and rewritten request bodies side by side.
  • state.json is bounded (20,000 entries, oldest evicted) and can be deleted safely; the worst case is that older turns get the "" fallback.
  • Note: the ~20 sessions created while testing this fix remain in copilot sessions history; they are harmless but can be deleted if unwanted.

7. Rollback

# launcher
sudo cp ~/.copilot-reasoning-fix/npm-loader.js.orig \
        /usr/local/lib/node_modules/@github/copilot/npm-loader.js
# reasoning display patch
cp ~/.copilot-reasoning-fix/app.js.orig \
   ~/.cache/copilot/pkg/linux-x64/1.0.88/app.js
# settings
cp ~/.copilot-reasoning-fix/settings.json.orig ~/.copilot/settings.json
cp ~/.copilot-reasoning-fix/copilot_config.orig ~/.copilot_config
# stop the proxy
pkill -f reasoning-proxy.py

Deleting ~/.copilot-reasoning-fix/ entirely also rolls everything back (keep the *.orig copies until the launcher and app.js are restored).


8. Limitations / caveats

  1. The proxy only understands the OpenAI chat-completions dialect (reasoning_content). One historical failure in your sessions was the Anthropic-shaped variant (400 The content[].thinking in the thinking mode must be passed back…). The current provider is OpenAI-compatible, so that path is not exercised; supporting it would mean adding content[].thinking block handling to the proxy.
  2. The proxy is a network hop on localhost. If it is down and cannot be restarted, the launcher falls back to talking to the provider directly (the original bug returns, but nothing else breaks).
  3. The tool-signature fallback can, in theory, attach the wrong reasoning text to a repeated identical tool call. Tool-call-id matching is always tried first; and since the API only requires a string, the consequence is at worst slightly off-context reasoning, never a failure.
  4. COPILOT_SHOW_REASONING overrides settings.json, so toggling reasoning in /settings will not stick while the variable is set. Unset it to hand control back.
  5. Reasoning text is only rendered by the interactive TUI. -p/non-interactive mode uses a separate renderer that never displays reasoning, regardless of settings.
  6. If the proxy needs a different interpreter, set COPILOT_REASONING_PROXY_PYTHON (Python 3.14 is what it is tested with here).

9. What a Copilot update will undo

  • copilot update / npm install -g @github/copilot replaces npm-loader.js and re-extracts the dist (new version directory), so both patches disappear.
  • The proxy, ~/.copilot-reasoning-fix/ and the ~/.copilot_config block survive; only the launcher and app.js edits need re-applying.
  • Before re-applying, re-check whether the upstream runtime has learned to round-trip reasoning for this path — if it has, the proxy is unnecessary and COPILOT_REASONING_PROXY=off is the cleanest end state.

Re-apply checklist for a new version X.Y.Z:

  1. Patch app.js in ~/.cache/copilot/pkg/linux-x64/X.Y.Z/ — re-locate function Ije(e,t){let n=e.banner?? and apply the same insertion.
  2. Re-apply the launcher patch (the file is small; the current version is a good starting point, only the surrounding minified code changes).
  3. Re-run the reproduction in §5a to confirm the fix is still needed and still works.

nolteb avatar Sep 26 '26 19:09 nolteb

@nolteb A couple of notes:

  • the reasoning_content is used only when using the OpenAI completion API
  • removing/suppressing the reasoning_content entirely produces lower-quality responses, which is pretty obvious.

There are two correct solutions:

  1. Using a proxy like the one I wrote. The proxy (specifically designed to manage the completion API preserves the reasoning_content field inside the content field and then restore it back inside the reasoning_content in the next turn.
  2. (only available only from a few weeks) Force copilot to use the new Responses API which are now supported by DeepSeek. This is done through the following environment variable:
export COPILOT_PROVIDER_WIRE_API="responses"

Solution 1 could be easily done by the Copilot, but I understand that this was never implemented. Solution 2 instead work by design because it is a different protocol which is already "reasoning-aware".

raffaeler avatar Sep 26 '26 19:09 raffaeler

@nolteb A couple of notes:

  • the reasoning_content is used only when using the OpenAI completion API
  • removing/suppressing the reasoning_content entirely produces lower-quality responses, which is pretty obvious.

I fed your critique into copilot and it verified its work with the following reaction:

The critic's claim appears incorrect in the main: the proxy does not remove reasoning; it adds the field Copilot omitted. And when Copilot does send reasoning, the proxy passes it through untouched (proven: injected=0, verbatim 194 chars).

I think I see what you mean though in the sense that sometimes the reasoning content isn't sent by copilot (the source of the error), when it should be including the content from the last response in the next turn. That's a problem which this solution doesn't solve. It does, however, prevent the immediate blocking problem of the 400 error ending the turn.

nolteb avatar Sep 26 '26 20:09 nolteb

The proxy is the one I wrote which moves the ’reasoning_content’ into ’content’ and back.

The note about removing is my general advice against those who claimed to fix the issue by removing the ’reasoning_content’ completely.

So the two are completely different things.

raffaeler avatar Sep 26 '26 21:09 raffaeler