Repository navigation
feat(backends): add default_extra_body to OpenAIBackend - #1536
Conversation
|
please don't review yet - resolving some issues |
|
Ready for review — issues from earlier are resolved: fixed a bug where |
…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]>
b85db95 to
10a7183
Compare
AngeloDanducci
left a comment
There was a problem hiding this comment.
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]>
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) |
5d130bf
Summary
Adds an optional
default_extra_body: dict | None = Noneparameter toOpenAIBackend.__init__().Fields set at construction time are merged into
extra_bodyon every request the backend makes, at lower priority than Mellea-assembled values and lower priority than per-callmodel_options.Closes #1453.
Scope note (read before reviewing): this fixes the session-wide default case —
default_extra_bodynow reliably reaches every request this backend makes, including calls that don't pass any per-callextra_bodyat all. It does not fix the older, already-documentedOpenAIBackend(model_options={"extra_body": ...})construction-time pattern, which goes through a different, shared merge path (resolve_model_options/merge_model_optionsinmellea/backends/_options.py) that does a flat replace rather than a deep merge — that pattern can still be silently clobbered by an unrelated per-callextra_bodyon some other call in the same session. That's a distinct bug with a wider blast radius (it's shared merge code also used byLocalHFBackend), tracked separately as #1539 rather than folded in here.Motivation
Callers that need a vendor-specific
extra_bodyfield on every request (for example,chat_template_kwargsfor thinking-tier control on vLLM-compatible servers) currently have no way to set it once. The only option is to pass it inmodel_optionsat everyinstruct()/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_thinkingchanges 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_kwargskey 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 actualthree-way test added in this PR, not a hypothetical):
At the session level, this is what lets a caller set
enable_thinkingonce 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 eitherone silently clobbering the other's
chat_template_kwargsentry.Merge order (lowest → highest priority)
default_extra_body— set at construction timebase— assembled by Mellea per request (documents, structured_outputs, adapter activation, …)user— from the caller's per-callmodel_optionschat_template_kwargsis deep-merged across all three layers so that, for example, a construction-timeenable_thinkingflag is not silently dropped when the request also carries anadapter_nameinjected by Mellea (intrinsic/adapter calls write their ownchat_template_kwargs.adapter_nameto activate the correct adapter).Changes
OpenAIBackend.__init__: new keyword-onlydefault_extra_bodyparam (defaultNone/{})_merge_user_extra_body: promoted from@staticmethodto instance method; now layersself._default_extra_bodyas the base; all existing call sites are unchanged_generate_from_chat_context_standard(the primary chat-completion path) only invoked_merge_user_extra_bodywhen a per-callextra_bodyoverride was present, sodefault_extra_bodywas 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.docs/docs/integrations/openai.md: the thinking-mode troubleshooting section now recommendsdefault_extra_bodyfor anything meant to persist across a session's calls, and flags the caveat on the oldermodel_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
test_merge_user_extra_body_none_returns_base: the method now always returns a new dict (can no longer return the originalbaseby identity sincedefault_extra_bodymust be layered in). Test updated to check equality rather than identity.test_default_extra_body_applied_when_no_per_call_overridetest_default_extra_body_overridden_by_per_calltest_default_extra_body_chat_template_kwargs_deep_mergedtest_default_extra_body_does_not_mutate_constructor_argtest_default_extra_body_chat_template_kwargs_three_way_merge— all three layers (default_extra_body,base, per-calluser) contributing tochat_template_kwargssimultaneously; 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 realgenerate_from_chat_contextpath 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_bodydefaults 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_optionsmerge so the oldermodel_options={"extra_body": ...}construction-time pattern gets the same deep-merge safety asdefault_extra_body, across all backends that use it (not justOpenAIBackend).