Skip to content

fix(backends): deep-merge extra_body.chat_template_kwargs in merge_model_options - #1617

Merged
planetf1 merged 3 commits into
generative-computing:mainfrom
planetf1:issue-1539
Sep 4, 2026
Merged

planetf1 merged 3 commits into
generative-computing:mainfrom
planetf1:issue-1539

Conversation

@planetf1

@planetf1 planetf1 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1539

Description

Impact

A backend-level extra_body default (e.g. a thinking-mode toggle set at construction time) gets silently dropped, with no error or warning, the moment a per-call extra_body is also present — even one that's about something else entirely:

backend = OpenAIBackend(
    model_id="gpt-4o", base_url="...",
    model_options={"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}},
)
# ... later, any call that passes its own unrelated extra_body:
m.instruct("...", model_options={"extra_body": {"some_unrelated_field": 123}})
# -> enable_thinking is gone. The model thinks again, silently.

This isn't a rare edge case: it fires on every intrinsic/adapter call, because OpenAIBackend._generate_from_intrinsic always builds its own extra_body (to write chat_template_kwargs["adapter_name"]) — so a session that sets a thinking default and also uses any adapter loses that default the first time the adapter is called.

Root cause

merge_model_options merges option dicts with a flat, top-level dict.update. That's correct for scalar options, but wrong for extra_body, whose value is itself a dict that different call sites write into independently.

Fix

merge_model_options now special-cases extra_body: when both sides have a dict there, their chat_template_kwargs sub-dicts deep-merge instead of one replacing the other (new ModelOption._merge_extra_body, mirroring the deep-merge OpenAIBackend._merge_user_extra_body already does one layer down). Every other option key — and every other key inside extra_body — still flat-overwrites exactly as before; only the one broken case changes.

Also updates docs/docs/integrations/openai.md, which documented this exact clobbering as a known limitation — that caveat is now gone.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

New tests:

  • test_model_options.py, test_options.py — unit coverage on the merge functions: unrelated-key preservation, chat_template_kwargs deep-merge, conflict resolution, non-mutation, non-dict fallback.
  • test_openai_intrinsics_unit.py::test_construction_time_extra_body_default_survives_intrinsic_call — the impact scenario end to end through a real intrinsic call: a construction-time thinking default, an unrelated per-call extra_body, and the adapter's own adapter_name all survive together in the final request. Confirmed this test fails on pre-fix code (KeyError: 'enable_thinking') and passes post-fix.

Full run: uv run pytest test/ -m "not qualitative" → 4286 passed, 20 skipped, 4 xpassed, 1 unrelated pre-existing flake (test_tracing_backend.py::test_stream_e2e, a live-Ollama timing assertion — passes on rerun). ruff format/ruff check/mypy clean; markdownlint-cli2 clean on the docs change.

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

…del_options

model_options-level extra_body defaults (the pattern documented for
setting a persistent thinking-mode toggle) were silently clobbered by
any unrelated per-call extra_body, because merge_model_options did a
flat dict.update on the whole extra_body value. Special-case extra_body
to deep-merge its chat_template_kwargs sub-dict, mirroring the deep-merge
OpenAIBackend._merge_user_extra_body already does; every other key
still flat-overwrites as before.

Adds regression tests at the merge-layer (test_model_options.py,
test_options.py) and through an actual intrinsic/adapter call
(test_openai_intrinsics_unit.py), plus a defensive fallback for a
malformed non-dict chat_template_kwargs. Updates the openai.md docs
section that previously documented this as a known limitation.

Fixes generative-computing#1539

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Sep 3, 2026
Comments and test docstrings should document current behavior, not
point back at the issue that motivated it. Also tightens the
_merge_extra_body/merge_model_options docstrings.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1
planetf1 marked this pull request as ready for review September 3, 2026 11:09
@planetf1
planetf1 requested a review from a team as a code owner September 3, 2026 11:09
Comment thread mellea/backends/model_options.py Resolved
…THINKING

extra_body passed via the generic model_options constructor dict (not
the dedicated default_extra_body param) was merged with per-call
extra_body inside _simplify_and_merge, then re-emerged downstream as
the highest-priority layer in _merge_user_extra_body (openai.py) and
the inline equivalent in litellm.py — silently outranking a per-call
ModelOption.THINKING resolved separately via the sentinel key.

Fold construction-time extra_body into the existing lowest-priority
_default_extra_body tier at __init__ instead, and exclude extra_body
from backend_defaults in _simplify_and_merge so it can no longer
re-enter the per-call precedence chain. Adds the same _default_extra_body
tier to litellm.py, which had none.

Adds regression tests in both backends that fail without the fix.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1
planetf1 added this pull request to the merge queue Sep 4, 2026
Merged via the queue into generative-computing:main with commit d29dc3e Sep 4, 2026
11 checks passed
@planetf1
planetf1 deleted the issue-1539 branch September 4, 2026 17:17
planetf1 added a commit that referenced this pull request Sep 10, 2026
- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on #1639/#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
planetf1 added a commit that referenced this pull request Sep 15, 2026
- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on #1639/#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request Sep 16, 2026
…-computing#1645)

* docs: document ModelOption.THINKING behaviour for Granite and add example

Adds a per-backend reasoning/thinking-mode reference table (native Ollama,
OpenAI/LiteLLM, HF) to configure-model-options.md, including Granite 4.2's
"low"-only effort distinction and its default-on behaviour, and cross-links
it from openai.md's existing Qwen3/vLLM extra_body section as the preferred
portable entry point. Adds docs/examples/thinking_mode.py demonstrating
THINKING=True/False/"low" against a local Ollama Granite model, verified
live against granite4.2:3b.

Fixes generative-computing#1638

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: describe THINKING doc as post-generative-computing#1639/generative-computing#1616 target state

Documents ModelOption.THINKING string levels and result.thinking as
supported on LocalHFBackend too, matching the state after generative-computing#1639 (HF string
forwarding fix) and generative-computing#1616 (HF <think> tag parsing) merge. Both are still
open — this docs PR should merge after them, or the LocalHFBackend row will
describe behaviour that isn't live yet.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: address code-review findings on THINKING docs

- Qualify the OpenAIBackend/LiteLLM `False` cell: real OpenAI reasoning
  models and non-Ollama LiteLLM targets never receive `reasoning_effort=
  "none"` (openai.py's server-type guard, litellm.py's ollama-prefix guard),
  so `False` does not actually disable thinking there.
- Add an inline callout marking the LocalHFBackend string-forwarding and
  result.thinking claims as contingent on generative-computing#1639/generative-computing#1616 merging, instead of
  only noting it in the PR description/comment.
- Correct "same mechanism as the OpenAI backend" — HF forwards through a
  gated chat-template variable, OpenAI sends an ungated top-level param.
- Note the runtime-forwarding dependency for the cross-backend Granite
  claim, add a `> Full example:` link, fix the unresolvable bare "generative-computing#1617"
  reference, add a non-determinism note to the new code block, drop the
  unnecessary `qualitative` marker, and fix US-English spelling in new
  content per CONTRIBUTING_DOCS.md.
- Add cheap assertions to docs/examples/thinking_mode.py pinning the
  documented per-arm behaviour; reran live against granite4.2:3b, all pass.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: fix stale THINKING dependency note now that generative-computing#1639 merged

generative-computing#1639 (HF string-forwarding) merged 2026-09-10; only generative-computing#1616
(result.thinking parsing) remains open. Also note that generative-computing#1616's
<think>-tag split is skipped for streaming (m serve) calls, per
its own PR description.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>

* docs: drop merged-PR references from THINKING doc

generative-computing#1616 and generative-computing#1639 have both merged, so LocalHFBackend's result.thinking
split is now current-main behaviour, not a future dependency.

Assisted-by: Claude Code
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Signed-off-by: Nigel Jones <[email protected]>

---------

Signed-off-by: Nigel Jones <[email protected]>
Co-authored-by: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(backends): model_options-level extra_body default is silently clobbered by an unrelated per-call extra_body

2 participants