-
Notifications
You must be signed in to change notification settings - Fork 707
fix(streaming): handle parameters.stream=true on model init #1926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adityasingh2400
wants to merge
1
commit into
NVIDIA-NeMo:develop
Choose a base branch
from
adityasingh2400:adityasingh2400/streaming-asyncstream-guard
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -406,7 +406,25 @@ def _prepare_model_kwargs(self, model_config): | |||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||
| dict: The prepared kwargs for model initialization | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| kwargs = model_config.parameters or {} | ||||||||||||||||||||||||||||||||||||||||||||||
| kwargs = dict(model_config.parameters) if model_config.parameters else {} | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Setting ``stream`` directly in ``parameters`` is a foot-gun: providers | ||||||||||||||||||||||||||||||||||||||||||||||
| # like OpenAI forward it verbatim to the HTTP client, which returns an | ||||||||||||||||||||||||||||||||||||||||||||||
| # ``AsyncStream`` object on every call. The non-streaming completion path | ||||||||||||||||||||||||||||||||||||||||||||||
| # then tries to ``.model_dump()`` that stream and crashes the server. | ||||||||||||||||||||||||||||||||||||||||||||||
| # See https://github.com/NVIDIA-NeMo/Guardrails/issues/1325. Streaming is | ||||||||||||||||||||||||||||||||||||||||||||||
| # opted in via the ``streaming`` rails-config flag (and requested per | ||||||||||||||||||||||||||||||||||||||||||||||
| # call via the API), not by baking ``stream: true`` into model params. | ||||||||||||||||||||||||||||||||||||||||||||||
| if kwargs.pop("stream", None): | ||||||||||||||||||||||||||||||||||||||||||||||
| log.warning( | ||||||||||||||||||||||||||||||||||||||||||||||
| "Ignoring `stream: true` set in `parameters` for model %r (engine %r). " | ||||||||||||||||||||||||||||||||||||||||||||||
| "Setting `stream` directly on a model causes the provider to return an " | ||||||||||||||||||||||||||||||||||||||||||||||
| "AsyncStream object on every call, which breaks the non-streaming path. " | ||||||||||||||||||||||||||||||||||||||||||||||
| "Request streaming via the API or set the `streaming` flag on the rails " | ||||||||||||||||||||||||||||||||||||||||||||||
| "config instead.", | ||||||||||||||||||||||||||||||||||||||||||||||
| getattr(model_config, "model", None), | ||||||||||||||||||||||||||||||||||||||||||||||
| getattr(model_config, "engine", None), | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+418
to
+427
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: nemoguardrails/rails/llm/llmrails.py
Line: 418-427
Comment:
The warning message hardcodes the literal string `` `stream: true` `` but the condition fires for any truthy value (e.g. `1`, a non-empty string). Embedding the actual value makes the message more actionable when a user sets a non-boolean truthy value.
```suggestion
stream_val = kwargs.pop("stream", None)
if stream_val:
log.warning(
"Ignoring `stream: %r` set in `parameters` for model %r (engine %r). "
"Setting `stream` directly on a model causes the provider to return an "
"AsyncStream object on every call, which breaks the non-streaming path. "
"Request streaming via the API or set the `streaming` flag on the rails "
"config instead.",
stream_val,
getattr(model_config, "model", None),
getattr(model_config, "engine", None),
)
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # If the optional API Key Environment Variable is set, add it to kwargs | ||||||||||||||||||||||||||||||||||||||||||||||
| if model_config.api_key_env_var: | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stream: falsemay confuse userskwargs.pop("stream", None)always removes thestreamkey regardless of its value, but the warning branch fires only when the value is truthy. This means a user who explicitly writesstream: falsein their config will see the key disappear from the effective kwargs with no log at any level. If they ever try to debug why the parameter is missing, there is nothing in the logs to explain it. Alogging.debug(...)line for the falsy path would make the behaviour fully observable without adding noise to normal operation.Prompt To Fix With AI