Skip to content

deepseek-v4-flash runs with thinking enabled by default (no extra_body override) — likely explains an observed JSON-parse failure on a semantic chunk #1621

Closed

Description

@sub4biz

Summary

graphify calls deepseek-v4-flash directly (bare model name, no extra_body) for semantic
extraction, but DeepSeek's own docs state the thinking toggle for this model defaults to enabled.
llm.py only disables thinking when "moonshot" in base_url (for Kimi) — there's no equivalent
branch for DeepSeek's base_url, so deepseek-v4-flash calls made by graphify likely run in thinking
mode unintentionally. We observed one JSON-parsing failure on a 216-chunk run whose symptoms (leaked
chain-of-thought-style text, near-max output token usage) are consistent with this — see the caveat in
"Evidence" below on what exactly we can and can't confirm about this specific occurrence.

Evidence

From an extract run on a ~28k-file monorepo (--backend deepseek --model deepseek-v4-flash):

[graphify] LLM returned invalid JSON, skipping chunk (first 200 chars: '-body.txt is a test resource, but we can include as data.

We need to be careful with node IDs: only lowercase, [a-z0-9_], no dots or slashes. So we need to transform paths.

For example: "some-project/su')
[graphify] deepseek returned a hollow response (content=no nodes/edges, output_tokens=34418); treating as truncation so adaptive retry can bisect the chunk.
[graphify] chunk of 63 truncated at depth 0, splitting into halves of 31 and 32

The leaked text ("We need to be careful with node IDs...") reads as chain-of-thought reasoning about
the extraction rules themselves, not extracted content — and it consumed 34,418 output tokens (near
the per-chunk cap) before (presumably) running out of budget before emitting the actual JSON payload.
Adaptive retry recovered by bisecting the chunk, but this cost extra latency/tokens and could plausibly
fail to recover on a smaller retry unit too.

Caveat: we cannot directly inspect what extra_body/thinking flag was actually sent for this
specific API call (no request-level logging was captured), so we can't prove with certainty that
thinking mode was on for this exact chunk — we're inferring it from (a) the content reading like
leaked reasoning rather than a malformed-but-direct answer, and (b) the confirmed fact that graphify's
code never disables thinking for this model, combined with DeepSeek's docs saying it's on by default.
This is a single observation (1 in 216 chunks) — we have not yet measured how often this recurs, on
this corpus or others.

Root cause

Per DeepSeek's current API documentation:

DeepSeek-V4-Flash supports both non-thinking and thinking (default) modes... the thinking
toggle defaults to enabled.

Mode is controlled via extra_body={"thinking": {"type": "enabled"|"disabled"}} (OpenAI SDK format).
The model-name aliases work as follows:

  • deepseek-chat → deepseek-v4-flash with thinking forced disabled
  • deepseek-reasoner → deepseek-v4-flash with thinking forced enabled
  • bare deepseek-v4-flash → thinking enabled by default, unless the caller passes extra_body

llm.py's request-building code has this structure:

if extra_body is not None:
    kwargs["extra_body"] = extra_body
elif "moonshot" in base_url:
    kwargs["extra_body"] = {"thinking": {"type": "disabled"}}

There's no equivalent elif for DeepSeek's base_url, so a bare --model deepseek-v4-flash call
falls through with no extra_body at all — leaving thinking on its documented default (enabled). A
comment elsewhere in the same backend's config block (# deepseek-chat / v4-flash (non-thinking) accept 0-2) states the opposite, but only deepseek-chat is actually forced non-thinking (via
DeepSeek's own alias resolution) — bare deepseek-v4-flash isn't.

Suggested fix

A straight copy of the moonshot pattern (checking only base_url) would be wrong here: DeepSeek's
deepseek-chat, deepseek-reasoner, and bare deepseek-v4-flash all share the same base_url, but
only the last one needs the override — deepseek-reasoner is the explicit opt-in for thinking mode
and shouldn't be silently disabled. Both model and backend are already in scope in this function,
so the new branch needs to key on model name, not just base_url:

elif backend == "deepseek" and model not in ("deepseek-chat", "deepseek-reasoner"):
    kwargs["extra_body"] = {"thinking": {"type": "disabled"}}

(We haven't verified whether deepseek-v4-pro has the same thinking-enabled-by-default behavior as
deepseek-v4-flash — DeepSeek's docs excerpt we found only confirms this for v4-flash by name, though
the same code example uses deepseek-v4-pro with an explicit extra_body override, suggesting the
same toggle applies. Worth confirming before assuming the fix covers both models identically.)

This would likely also reduce token spend and latency across extraction runs generally, since
thinking tokens are billed as output tokens and add processing time even when they don't cause an
outright parse failure.

Broader suggestion: the underlying problem is that whether-to-disable-thinking is currently
scattered as ad-hoc elif branches in _call_openai_compat (one exists for moonshot, none for
deepseek) — that's exactly the kind of gap that's easy to introduce silently when a new backend is
added, as apparently happened here. Since BACKENDS is already a config dict per backend, adding an
explicit field there (e.g. "thinking_default": "enabled" | "disabled" | None) and reading it in one
place, instead of one elif per provider, would make a missing case visible in the data instead of
silently falling through in the code.

This doesn't fully solve it on its own, though: not every thinking-capable model can actually be
switched off — deepseek-reasoner and presumably similar explicit reasoning aliases from other
providers have no disable path at all, since reasoning is the point of picking that model. So the
config would need a third state alongside enabled/disabled, e.g. "thinking_default": "not_disableable", for models where no override exists. graphify could then check that field once at
startup and print a warning only for that specific case — models resolved to "enabled" or
"disabled" run silently as now; only a "not_disableable" model triggers something like "model X
keeps reasoning active with no way to disable it; extraction JSON may occasionally leak reasoning text
and chunk-to-chunk output can be less deterministic than usual." That way the warning stays meaningful
instead of becoming noise the user learns to ignore, and still tells them upfront why a rebuild might
behave differently between runs, rather than them discovering it via a parse failure partway through a
paid extraction.

Environment

  • graphifyy 0.9.5
  • backend deepseek, model deepseek-v4-flash, --token-budget 60000 --max-concurrency 32
  • Observed once in 216 chunks on this run; frequency not yet measured

Activity

  1. safishamsi commented on Jul 3, 2026

    @safishamsi
    Member

    Confirmed the code path: llm.py disables thinking only for "moonshot" in base_url (Kimi) — there's no equivalent branch for DeepSeek's base_url, at either of the two extra_body sites. So graphify sends no thinking toggle for deepseek-v4-flash.

    Worth flagging a contradiction your report surfaces: the built-in deepseek provider config carries the comment "deepseek-chat / v4-flash (non-thinking) accept 0-2" — i.e. the config was written assuming v4-flash is a non-thinking model. Your reading of DeepSeek's docs (thinking defaults enabled) says otherwise. I don't want to bake in a extra_body disable-thinking param for an external API on a guess when our own assumption and your docs disagree — a wrong extra_body key can 400 the request or silently no-op.

    Two things:

    1. Immediate workaround (no code change needed): the deepseek path already honors an extra_body override via a custom provider in providers.json. You can set the exact disable-thinking body DeepSeek documents and it'll be sent verbatim (it takes precedence over the moonshot default).
    2. To land it as the built-in default, could you confirm the exact request shape from DeepSeek's docs — the precise key/value to disable thinking for deepseek-v4-flash (e.g. is it extra_body={"thinking": {"type": "disabled"}}, a chat_template_kwargs flag, or something else)? With that confirmed I'll add it to the built-in deepseek config and fix the stale "non-thinking" comment in the same change.

    The observed symptom (leaked reasoning text, ~34k output tokens, hollow-response bisection) is consistent with thinking-on, so this is very likely the cause — I just want the param right rather than shipped-and-guessed. Thanks for the detailed evidence.

  2. sub4biz commented on Jul 3, 2026

    @sub4biz
    Author

    Verified the exact request shape empirically against the live DeepSeek API (https://api.deepseek.com/chat/completions), same trivial prompt each time ("What is 2+2? Answer with just the number.", max_completion_tokens: 300):

    1. No override (current graphify behavior for bare deepseek-v4-flash):

    {"model": "deepseek-v4-flash", "messages": [...], "max_completion_tokens": 300}

    Response includes reasoning:

    "message": {"content": "4", "reasoning_content": "We need to answer with just the number. 2+2=4."},
    "usage": {..., "completion_tokens_details": {"reasoning_tokens": 16}}

    2. {"thinking": {"type": "disabled"}} merged at the top level of the request body — this is exactly what the OpenAI Python SDK's extra_body={"thinking": {"type": "disabled"}} kwarg produces on the wire (same mechanism your moonshot branch already uses):

    {"model": "deepseek-v4-flash", "messages": [...], "max_completion_tokens": 300, "thinking": {"type": "disabled"}}

    Response has no reasoning at all:

    "message": {"content": "4"},
    "usage": {"completion_tokens": 1, ...}   // no reasoning_tokens key at all

    3. For comparison — a literal "extra_body": {...} JSON key (i.e. what you'd get if something sent extra_body as a literal wire-level field instead of letting the SDK unpack it):

    {"model": "deepseek-v4-flash", "messages": [...], "extra_body": {"thinking": {"type": "disabled"}}}

    This silently no-ops — reasoning stays on, identical to case 1. So the risk here isn't a 400, it's a silent no-op if the shape is wrong — worth double-checking the actual outgoing request if this gets added, but extra_body={"thinking": {"type": "disabled"}} passed through the OpenAI SDK (the same way the moonshot branch already does it) reproduces case 2, not case 3, since the SDK flattens extra_body's contents into the top-level body itself.

    So: extra_body={"thinking": {"type": "disabled"}} is confirmed correct and safe — no 400, no silent no-op, and it's the identical mechanism already proven in your kimi/moonshot branch. Re: the stale "non-thinking" comment — that's just wrong for bare deepseek-v4-flash; happy to be wrong about why it was written that way, but the current documented+empirical behavior is thinking-enabled-by-default as described in the issue.

    Also checked deepseek-v4-pro with the same test — identical pattern: no override → reasoning_content + reasoning_tokens: 26; {"thinking": {"type": "disabled"}} → no reasoning, completion_tokens: 1. So the fix condition can safely cover both deepseek-v4-flash and deepseek-v4-pro.

  3. sub4biz commented on Jul 6, 2026

    @sub4biz
    Author

    @safishamsi — I ran the suggested extra_body={"thinking": {"type": "disabled"}} override in
    production over several full rebuilds of a large, mixed real-world corpus (code, docs, YAML/Helm
    configs, ~40k files), comparing patched (thinking off) vs unpatched (thinking on) across multiple
    repeated runs on a frozen corpus snapshot, to separate the patch's effect from ordinary run-to-run
    noise.

    What the patch fixes, confirmed: the original hollow-response / reasoning-leak failure mode (model
    emits prose like "Let's write...", "We'll also include..." instead of JSON) is real and reproducible on
    the unpatched path — patched runs never hit it. So the underlying diagnosis in this issue is correct and
    the failure mode is worth guarding against.

    What surprised us, though:

    • Patched runs actually had more total truncation/invalid-JSON incidents overall than unpatched
      (roughly 30x more), just of a different, more benign character — clipped valid JSON at the token
      limit, not a reasoning-leak dump. So disabling thinking doesn't reduce failures in aggregate; it
      changes a rare-but-dangerous failure into a frequent-but-recoverable one.
    • On blind quality comparison of extracted labels/entities on real text content, unpatched (thinking on)
      was systematically more accurate than patched — it correctly picked up details patched missed or
      mechanically echoed from filenames instead of interpreting content. This was confirmed independently
      by a second blind reviewer without knowledge of which side was "supposed" to win.
    • Unpatched also had measurably better file coverage — some files produced zero semantic nodes under the
      patched path but meaningful nodes under thinking-on.

    So the two modes trade stability/predictability (patched) for extraction quality/coverage (unpatched) —
    it isn't a strict improvement either way.

    Suggestion: rather than hardcoding thinking-disabled as the default for deepseek-v4-flash
    (mirroring the Kimi branch), consider exposing thinking mode as an explicit, documented user-facing
    config flag — default left as the model's documented behavior (or decided separately with more data),
    with the trade-off called out in the docs so users can pick based on whether they value
    reproducibility/robustness or extraction quality more. Separately, it'd be worth hardening the
    JSON-parsing/retry path so a reasoning-leak response gets caught and retried rather than surfacing as a
    hard extraction failure — that addresses the original safety concern without forcing thinking off for
    everyone.

    Happy to share the methodology/more detail if useful — this was a fairly thorough internal test
    (multiple repeated runs to establish a noise floor, blind quality scoring, coverage diffing) but I kept
    specifics generic here since the corpus is private.

  4. safishamsi commented on Jul 6, 2026

    @safishamsi
    Member

    Thank you for the unusually thorough testing here — the wire-level verification of the exact request shape, and especially the production A/B with blind quality scoring and coverage diffing, is exactly what was needed to avoid shipping the wrong fix.

    Your data changed the decision. The naive fix (mirror the kimi branch, force thinking off for deepseek) would have been a regression: you showed disabling thinking removes the rare reasoning-leak failure but trades it for far more frequent benign truncation and measurably lower extraction quality and file coverage. And the leak itself is already recoverable — the adaptive extraction/labeling retry splits and re-requests a chunk whose response wasn't valid JSON, so a leak is a rare, self-healing event rather than a hard failure.

    So on v8 (5d01373):

    • Thinking stays ON by default for deepseek (quality/coverage win).
    • Added an opt-in GRAPHIFY_DISABLE_THINKING=1 for users who prefer run-to-run stability over extraction quality — it sends {"thinking": {"type": "disabled"}} at both the extraction and labeling call sites for reasoning-capable OpenAI-compatible backends. An explicit providers.json extra_body still wins over it, and the moonshot/kimi branch is unchanged.
    • Fixed the stale "non-thinking" comment on the built-in deepseek config — v4-flash and v4-pro are thinking-enabled by default, as you documented.

    The tradeoff is called out on the flag itself. Closing as resolved; reopen if the default doesn't hold up on your corpus.

  5. added a commit that references this issue on Jul 6, 2026
  6. deleted a comment from on Jul 7, 2026
  7. deleted a comment from on Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions