Skip to content

fix: remove stream_usage from text completion #1285

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
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Pouyanpi
Copy link
Collaborator

This pull request includes a small but important change to the _init_text_completion_model function in langchain_initializer.py. The change ensures compatibility with text completion APIs by removing the unsupported stream_usage parameter from the kwargs dictionary before initializing the provider class.

TODO:

ensure current implementation for token usage tracking does not break for other providers see #1264

@Pouyanpi Pouyanpi added this to the v0.15.0 milestone Jul 10, 2025
@Pouyanpi Pouyanpi self-assigned this Jul 10, 2025
@Pouyanpi Pouyanpi added the bug Something isn't working label Jul 10, 2025
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.79%. Comparing base (ef97795) to head (1ec9fc6).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1285   +/-   ##
========================================
  Coverage    69.78%   69.79%           
========================================
  Files          161      161           
  Lines        16057    16058    +1     
========================================
+ Hits         11206    11207    +1     
  Misses        4851     4851           
Flag Coverage Δ
python 69.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
nemoguardrails/llm/models/langchain_initializer.py 89.51% <100.00%> (+0.08%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Pouyanpi Pouyanpi requested a review from Copilot July 10, 2025 15:11
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes the unsupported stream_usage parameter from the kwargs before initializing text completion providers to prevent API errors.

  • Strip out stream_usage so text completion calls don’t receive an invalid argument.
  • Ensures compatibility with providers like OpenAI’s AsyncCompletions.
Comments suppressed due to low confidence (1)

nemoguardrails/llm/models/langchain_initializer.py:258

  • Add a unit test to verify that passing stream_usage into _init_text_completion_model no longer raises an unexpected keyword argument error, ensuring this change is covered.
    kwargs.pop("stream_usage", None)

@@ -253,6 +253,9 @@ def _init_text_completion_model(
if provider_cls is None:
raise ValueError()
kwargs = _update_model_kwargs(provider_cls, model_name, kwargs)
# remove stream_usage parameter as it's not supported by text completion APIs
# (e.g., OpenAI's AsyncCompletions.create() doesn't accept this parameter)
kwargs.pop("stream_usage", None)
Copy link
Preview

Copilot AI Jul 10, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the "stream_usage" key into a shared constant or documenting it in the function docstring to avoid magic strings and make the removal rationale more discoverable.

Suggested change
kwargs.pop("stream_usage", None)
kwargs.pop(STREAM_USAGE_KEY, None)

Copilot uses AI. Check for mistakes.

@Pouyanpi Pouyanpi marked this pull request as ready for review July 11, 2025 12:25
@Pouyanpi Pouyanpi requested review from tgasser-nv and trebedea July 11, 2025 12:25
Copy link
Collaborator

@trebedea trebedea left a comment

Choose a reason for hiding this comment

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

This is a good fix for now, but we should try to find something more generic for unsupported attributes in kwargs for Langchain LLM providers. Let's merge this and talk in private about a generic fix.

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.

3 participants