fix(iorails): Apply inference-time llm_params on top of Model.parameters in ModelEngine#2020
Conversation
….parameters (after filtering out reserved parameters)
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR fixes a bug where
|
| Filename | Overview |
|---|---|
| nemoguardrails/guardrails/model_engine.py | Adds _RESERVED_LLM_PARAMETERS frozenset and body_param_defaults (immutable MappingProxyType) to ModelEngine.__init__, correctly filtering transport/secret/streaming keys from Model.parameters before exposing them as per-request body defaults. |
| nemoguardrails/guardrails/engine_registry.py | Both model_call and stream_model_call now compute merged_params = {**engine.body_param_defaults, **kwargs} before passing to the engine and span attributes, correctly giving per-call kwargs priority over config-level defaults. |
| tests/guardrails/test_model_engine.py | New TestModelEngineBodyParamDefaults class covers all reserved-key categories (transport, secret, streaming-control, identity, client-only) and the sampling-pass-through case with isolated unit tests. |
| tests/guardrails/test_engine_registry.py | New TestEngineRegistryParameterDefaults class covers both non-streaming and streaming paths including precedence ordering, reserved-key exclusion, and the stream duplicate-keyword guard. |
Sequence Diagram
sequenceDiagram
participant Caller
participant EngineRegistry
participant ModelEngine
Note over Caller,ModelEngine: Before this PR — Model.parameters dropped
Caller->>EngineRegistry: "model_call(model_type, messages, **llm_params)"
EngineRegistry->>ModelEngine: "chat_completion(messages, **llm_params)"
Note right of ModelEngine: Model.parameters ignored
Note over Caller,ModelEngine: After this PR — parameters merged
Caller->>EngineRegistry: "model_call(model_type, messages, **llm_params)"
EngineRegistry->>EngineRegistry: "merged = {**body_param_defaults, **llm_params}"
Note right of EngineRegistry: body_param_defaults = Model.parameters minus _RESERVED_LLM_PARAMETERS
EngineRegistry->>ModelEngine: "chat_completion(messages, **merged)"
Note right of ModelEngine: Config defaults + per-call overrides both applied
Reviews (3): Last reviewed commit: "Add default_headers and default_query to..." | Re-trigger Greptile
📝 WalkthroughWalkthroughModel parameter defaults are now computed and filtered during engine initialization, retaining only sampling/body parameters while excluding reserved transport and streaming keys. When EngineRegistry invokes model calls, it merges these model-configured defaults with per-call kwargs, with per-call values taking precedence, and applies the merged parameters to both LLM span request attributes and the actual chat-completion call. ChangesModel Parameter Defaults and Merging
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Pouyanpi
left a comment
There was a problem hiding this comment.
LGTM 👍🏻
a side note:
IORails does not use the shared OpenAI-compatible client yet, so I don’t think we need to fully support default_headers or default_query here. But since these are client only options, can we add them to the reserved (filter) list so they don’t get forwarded as chat completion body fields? that keeps current behavior safe and leave proper client wiring for a (potential) future refactor.
Description
IORails was silently dropping
llm_paramssampling fields configured in a model's parameters (i.e.Model.parameters). It tookGenerationOptions.llm_paramsat inference-time only.This PR filters out reserved parameters (
_RESERVED_LLM_PARAMETERS) in theModel.parametersdict and stores these per-model so they're used on every call. IfGenerationOptions.llm_paramskeys are provided they overwrite theModel.parametersvalues.Related Issue(s)
#2009 Introduced LLM span attributes including parameters.
Test Plan
Pre-commit
Unit-test
Checklist