Skip to content

feat(backends): add default_extra_body to OpenAIBackend - #1536

Merged
planetf1 merged 4 commits into
generative-computing:mainfrom
planetf1:fix/1453-default-extra-body
Aug 13, 2026
Merged

planetf1 merged 4 commits into
generative-computing:mainfrom
planetf1:fix/1453-default-extra-body

Conversation

@planetf1

@planetf1 planetf1 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds an optional default_extra_body: dict | None = None parameter to OpenAIBackend.__init__().

Fields set at construction time are merged into extra_body on every request the backend makes, at lower priority than Mellea-assembled values and lower priority than per-call model_options.

Closes #1453.

Scope note (read before reviewing): this fixes the session-wide default case — default_extra_body now reliably reaches every request this backend makes, including calls that don't pass any per-call extra_body at all. It does not fix the older, already-documented OpenAIBackend(model_options={"extra_body": ...}) construction-time pattern, which goes through a different, shared merge path (resolve_model_options / merge_model_options in mellea/backends/_options.py) that does a flat replace rather than a deep merge — that pattern can still be silently clobbered by an unrelated per-call extra_body on some other call in the same session. That's a distinct bug with a wider blast radius (it's shared merge code also used by LocalHFBackend), tracked separately as #1539 rather than folded in here.

Motivation

Callers that need a vendor-specific extra_body field on every request (for example, chat_template_kwargs for thinking-tier control on vLLM-compatible servers) currently have no way to set it once. The only option is to pass it in model_options at every instruct() / act() call site, which is verbose and error-prone in any wrapper code.

There's a second, stronger reason to set this once rather than per call: on servers like vLLM, chat_template_kwargs.enable_thinking changes the rendered chat-template prefix, not just a sampling parameter. Toggling it call-to-call can invalidate that server's prefix/KV cache for the conversation from that point on. Setting it once at construction keeps the template shape — and the cache — stable for the whole session.

Example use case

The scenario this deep-merge exists for: a session that sets a thinking
default once at construction, and also makes calls that activate an
intrinsic/aLoRA adapter (which independently writes into the same
chat_template_kwargs key to activate itself). Without the deep merge,
whichever of the two got assembled last would silently overwrite the
other's entry.

Verified directly against _merge_user_extra_body (this is the actual
three-way test added in this PR, not a hypothetical):

backend = OpenAIBackend(
    model_id="gpt-4o", base_url="http://localhost:9999/v1", api_key="test-key",
    default_extra_body={"chat_template_kwargs": {"enable_thinking": True}},
)

merged = backend._merge_user_extra_body(
    {"chat_template_kwargs": {"adapter_name": "answerability"}},  # Mellea's own adapter-activation write
    {"chat_template_kwargs": {"thinking_budget": 5000}},           # a per-call override
)
assert merged["chat_template_kwargs"] == {
    "enable_thinking": True,       # from default_extra_body — survives
    "adapter_name": "answerability",  # from Mellea's adapter activation — survives
    "thinking_budget": 5000,       # from the per-call override — wins on conflicts
}

At the session level, this is what lets a caller set enable_thinking
once when constructing the backend and then freely mix ordinary
instruct()/act() calls with calls that use an intrinsic adapter
(mellea.stdlib.components.intrinsic) in the same session, without either
one silently clobbering the other's chat_template_kwargs entry.

Merge order (lowest → highest priority)

  1. default_extra_body — set at construction time
  2. base — assembled by Mellea per request (documents, structured_outputs, adapter activation, …)
  3. user — from the caller's per-call model_options

chat_template_kwargs is deep-merged across all three layers so that, for example, a construction-time enable_thinking flag is not silently dropped when the request also carries an adapter_name injected by Mellea (intrinsic/adapter calls write their own chat_template_kwargs.adapter_name to activate the correct adapter).

Changes

  • OpenAIBackend.__init__: new keyword-only default_extra_body param (default None / {})
  • _merge_user_extra_body: promoted from @staticmethod to instance method; now layers self._default_extra_body as the base; all existing call sites are unchanged
  • Fix: _generate_from_chat_context_standard (the primary chat-completion path) only invoked _merge_user_extra_body when a per-call extra_body override was present, so default_extra_body was silently dropped on any ordinary call that didn't also pass one — defeating the "set once at construction" point of this feature. The merge is now unconditional at that call site.
  • Updated docstrings
  • docs/docs/integrations/openai.md: the thinking-mode troubleshooting section now recommends default_extra_body for anything meant to persist across a session's calls, and flags the caveat on the older model_options={"extra_body": ...}-at-construction pattern (see fix(backends): model_options-level extra_body default is silently clobbered by an unrelated per-call extra_body #1539)

Tests

  • Updated test_merge_user_extra_body_none_returns_base: the method now always returns a new dict (can no longer return the original base by identity since default_extra_body must be layered in). Test updated to check equality rather than identity.
  • Added:
    • test_default_extra_body_applied_when_no_per_call_override
    • test_default_extra_body_overridden_by_per_call
    • test_default_extra_body_chat_template_kwargs_deep_merged
    • test_default_extra_body_does_not_mutate_constructor_arg
    • test_default_extra_body_chat_template_kwargs_three_way_merge — all three layers (default_extra_body, base, per-call user) contributing to chat_template_kwargs simultaneously; the other tests each only covered two of the three layers at once.
    • test_standard_chat_path_applies_default_extra_body_without_per_call_override — regression test for the call-site bug above, exercising the real generate_from_chat_context path with a mocked client. Verified it fails against the pre-fix code (KeyError: 'extra_body') and passes with the fix.

Backward compatibility

Fully additive — default_extra_body defaults to {} and the merge is a no-op when it is empty. No existing callers are affected.

Follow-up

#1539 tracks fixing the shared resolve_model_options/merge_model_options merge so the older model_options={"extra_body": ...} construction-time pattern gets the same deep-merge safety as default_extra_body, across all backends that use it (not just OpenAIBackend).

@planetf1
planetf1 requested a review from a team as a code owner August 11, 2026 16:26
@github-actions github-actions Bot added the enhancement New feature or request label Aug 11, 2026
@planetf1
planetf1 marked this pull request as draft August 11, 2026 16:31
@planetf1

Copy link
Copy Markdown
Contributor Author

please don't review yet - resolving some issues

@planetf1

Copy link
Copy Markdown
Contributor Author

Ready for review — issues from earlier are resolved: fixed a bug where default_extra_body was silently dropped without a per-call override, added regression tests, updated the OpenAI integration doc, and filed #1539 as a separate follow-up for the older model_options={"extra_body": ...} construction-time pattern (out of scope here).

…omputing#1453)

Add an optional construction-time default_extra_body parameter to
OpenAIBackend. Fields set here are merged into every request the backend
makes, at lower priority than Mellea-assembled values and at lower
priority than per-call model_options.

This enables callers that need a vendor-specific extra_body field on
every request (e.g. vLLM chat_template_kwargs for thinking-tier control)
to set it once at construction rather than repeating it at every
instruct() / act() call site.

Merge order (lowest → highest priority):
  1. default_extra_body  — construction time
  2. base               — assembled by Mellea per request
  3. user               — from caller model_options

chat_template_kwargs is deep-merged across all three layers so that,
for example, a construction-time enable_thinking flag is preserved
when the request also carries an adapter_name injected by Mellea.

_merge_user_extra_body is promoted from @staticmethod to an instance
method to gain access to self._default_extra_body. All existing call
sites already call it as self._merge_user_extra_body(), so no
call-site changes are needed.

Tests:
- Updated test_merge_user_extra_body_none_returns_base: method now
  always returns a new dict; test updated to check equality not identity.
- Added 4 new tests covering: default applied with no override,
  per-call override wins, chat_template_kwargs deep-merged across
  default+base layers, constructor arg not mutated.

Resolves: generative-computing#1453
Signed-off-by: Nigel Jones <[email protected]>
_generate_from_chat_context_standard only called _merge_user_extra_body
when the caller passed a per-call extra_body override, so a
construction-time default_extra_body was silently dropped on an
ordinary call — defeating the "set once at construction" point of generative-computing#1453.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
…(openai): recommend default_extra_body for thinking-tier defaults

Add a regression test exercising default_extra_body, Mellea's own
per-request extra_body, and a per-call override all contributing to
chat_template_kwargs simultaneously — the prior tests only covered pairs
of these three layers.

Update the thinking-mode section of the OpenAI integration doc to
recommend default_extra_body for anything meant to persist across a
session's calls, and to flag that the older
model_options={"extra_body": ...} construction-time pattern is not
safe against an unrelated per-call extra_body override (see generative-computing#1539).

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

@AngeloDanducci AngeloDanducci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, though looks like its failing on double backticks used as markup.

RST-style double-backtick notation fails the Markdown/MDX docstring
compatibility check (tooling/docs-autogen/test_validate.py), which was
failing CI on the 3.12 quality job.

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

Copy link
Copy Markdown
Contributor Author

Generally LGTM, though looks like its failing on double backticks used as markup.

Ah. - fixed (I had a distinct issue open to fix some of the remaining rst issues as they are much wider than this change - but this one failed here so fixed up the file)

@planetf1
planetf1 enabled auto-merge August 13, 2026 10:15
@planetf1
planetf1 added this pull request to the merge queue Aug 13, 2026
Merged via the queue into generative-computing:main with commit 5d130bf Aug 13, 2026
10 checks passed
@planetf1
planetf1 deleted the fix/1453-default-extra-body branch August 13, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(backends): add default_extra_body to OpenAIBackend for vLLM/vendor-specific per-request fields

2 participants