AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ChatGroq Accepts N > 1 For Non-Streaming Requests Although Groq Only Supports N=1

ChatGroq in langchain-groq allows n=2 during construction for non-streaming requests, sending an invalid parameter to the Groq API which returns HTTP 400.

mediumConfidence 92%Langchain-GroqAffected V1.1.3

Origin Analysis

ChatGroq's validation logic rejects n < 1 and rejects n > 1 only when streaming is enabled; the non-streaming path skips the n > 1 check, so the unsupported n=2 is sent to Groq, causing a late API failure.
1. Set a valid GROQ_API_KEY.\n2. Instantiate ChatGroq(model='openai/gpt-oss-20b', n=2, max_retries=0).\n3. Observe that construction succeeds and self.n is 2.\n4. Call invoke('Reply with exactly: hi').\n5. The call raises groq.BadRequestError with status 400 and error message: "'n' : number must be at most 1".

Fixing Code Block

from pydantic import model_validator # Add this method inside the ChatGroq class (e.g., after field definitions) @model_validator(mode="after") def validate_n_supported(self) -> "ChatGroq": if self.n != 1: raise ValueError("Groq's Chat Completions API currently only supports n=1") return self # Optionally remove or simplify the existing streaming-specific n > 1 check in validate_environment, # as the model_validator above already enforces n == 1 for all modes.
The model_validator(mode='after') runs after all fields are set and before any API call, validating n == 1 for both streaming and non-streaming configurations. This prevents invalid requests from reaching the Groq API. The existing environment validator can be simplified to avoid redundant streaming-specific checks.

Edge Case Audit

This change makes invalid configuration fail earlier (constructor-time) instead of on first API call, which may break code that previously constructed ChatGroq with n=2 but never invoked it. If Groq later adds support for n > 1, this hard constraint must be relaxed. Rollback suggestion: revert the new validator and restore the original streaming-only n > 1 check, or pin langchain-groq to a version before this change.

Ecosystem Topology