How nGPT Handles Model-Specific Parameter Constraints and Temperature Restrictions
nGPT centralizes model-specific parameter validation in the NGPTClient class, automatically enforcing temperature restrictions for models like GPT-5, O1, O3, and O4 that only support temperature=1.0, while allowing full range customization for other models.
The nazdridoy/ngpt repository provides a unified CLI and Python API for interacting with various OpenAI-compatible models. When working with diverse model families, developers face a common challenge: different models enforce different parameter constraints. This article examines how nGPT implements centralized validation to handle model-specific temperature restrictions automatically.
Centralized Validation Architecture
The core validation logic resides in the NGPTClient class within ngpt/api/client.py. Rather than scattering checks across multiple CLI modes, nGPT consolidates all model-specific constraint enforcement in a single location before constructing the API payload.
Temperature Restriction Logic (Lines 78-101)
Around lines 78-101 of ngpt/api/client.py, the client implements a three-tier validation system:
-
Explicit Flag Detection: The code inspects
sys.argvto determine if the user explicitly passed--temperature(temperature_explicitly_set = '--temperature' in sys.argv). -
Model Family Classification: A hard-coded list defines restricted families:
temp_restricted_models = ["gpt-5", "o1", "o3", "o4"]. These models only supporttemperature=1.0. -
Constraint Enforcement: If
model_temp_restrictedevaluates to true, the client validates the requested temperature value against the allowed range.
Handling Violations and Valid Requests
When a restricted model is detected, the client branches into specialized handling:
Error Scenario: If the user explicitly sets a temperature other than 1.0 for a restricted model, the client prints a descriptive error and exits:
if temperature_explicitly_set and temperature != 1.0:
print(f"\n\nError: {model_family.upper()} models only support temperature=1 (default).")
sys.exit(1)
Valid Scenario: If the user explicitly sets temperature=1.0 or omits the flag entirely, the client either adds the valid value to the payload or omits it to use the API default.
CLI Integration and Default Values
The command-line interface defines temperature parameters in ngpt/cli/args.py (lines 51-57), setting a default of 0.7 and validating the range 0.0-2.0. However, this generic validation occurs before model-specific constraints are applied.
The parsed value flows through various CLI modes (text, chat, interactive) in ngpt/cli/modes/ and ultimately reaches NGPTClient.chat(). This architecture ensures that model-specific validation happens at the last possible moment, when the actual model identity is known.
Practical Implementation Examples
Standard Model with Custom Temperature
For unrestricted models like gpt-3.5-turbo, you can specify any valid temperature:
from ngpt.api.client import NGPTClient
client = NGPTClient(api_key="sk-xxxx", model="gpt-3.5-turbo")
response = client.chat(
prompt="Write a haiku about clouds.",
temperature=0.9,
stream=False
)
print(response)
The client includes "temperature": 0.9 in the API payload without restriction.
Attempting Invalid Temperature on Restricted Models
When using a restricted model like gpt-5 with a non-default temperature:
ngpt --model gpt-5 --temperature 0.5 "Explain quantum tunneling"
The client detects the conflict and exits immediately:
Error: GPT-5 models only support temperature=1 (default).
No HTTP request is sent to the API, preventing runtime errors from the provider.
Valid Configuration for Restricted Models
To use a restricted model correctly, either omit the temperature flag or explicitly set it to 1.0:
ngpt --model gpt-5 --temperature 1 "Summarize Macbeth"
The client accepts this configuration and includes the parameter in the request payload.
Summary
- Centralized Validation: nGPT consolidates model-specific constraint checking in
NGPTClientwithinngpt/api/client.py, preventing duplicate logic across CLI modes. - Hard-coded Restrictions: The client maintains a specific list of restricted families (
gpt-5,o1,o3,o4) that only supporttemperature=1.0. - Explicit Flag Detection: By inspecting
sys.argv, the client distinguishes between default values and user-specified parameters, providing accurate error messages. - Fail-Fast Behavior: Invalid configurations trigger immediate exit with descriptive error messages before any API request is attempted, saving tokens and reducing latency.
Frequently Asked Questions
Which models are affected by temperature restrictions in nGPT?
The restricted model families include gpt-5, o1, o3, and o4. Any model name starting with these prefixes (such as gpt-5-turbo or o1-preview) triggers the temperature validation logic that enforces temperature=1.0 exclusively.
Why does nGPT check sys.argv instead of just comparing against the default value?
nGPT inspects sys.argv to determine if the user explicitly passed the --temperature flag versus accepting the CLI default of 0.7. This distinction allows the client to provide precise error messaging: it can inform users that they specifically requested an incompatible value, rather than suggesting they change a default they never touched.
What happens if I try to use temperature 1.0 with a restricted model?
If you explicitly set --temperature 1.0 for a restricted model like gpt-5, nGPT accepts the value and includes it in the API payload. If you omit the flag entirely, the client leaves the temperature parameter out of the request, allowing the API to apply its own default. Both approaches result in successful API calls.
How can I add support for a new model family with temperature restrictions?
To extend nGPT for a new restricted model family, modify the temp_restricted_models list in ngpt/api/client.py (around line 84) to include the new prefix. The existing validation logic will automatically enforce temperature=1.0 for any model starting with that prefix, without requiring changes to the CLI argument parsing or individual mode implementations.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →