Skip to content

Fix OpenAI o1 model parameter translation (resolves #8)#11

Merged
mikehostetler merged 6 commits into
mainfrom
feature/unique-model-provider-options
Sep 15, 2025
Merged

Fix OpenAI o1 model parameter translation (resolves #8)#11
mikehostetler merged 6 commits into
mainfrom
feature/unique-model-provider-options

Conversation

@mikehostetler

Copy link
Copy Markdown
Contributor

Overview

This PR resolves Issue #8 by implementing automatic parameter translation for OpenAI o1 models that require different parameter names than the standard ReqLLM API.

Problem Solved

OpenAI o1 models have unique parameter requirements:

  • Use max_completion_tokens instead of max_tokens
  • Don't support temperature parameter (must be omitted entirely)

Previously, using standard ReqLLM parameters with o1 models would result in API errors.

Solution

1. Added Provider Options Translation System

  • Extended ReqLLM.Provider behavior with optional translate_options/3 callback
  • Added translation helper functions to ReqLLM.Provider.DSL
  • Integrated translation step into generation pipeline

2. Implemented OpenAI o1 Translation

  • max_tokensmax_completion_tokens for o1 models
  • temperature parameter dropped with warning for o1 models
  • Non-o1 models remain unchanged

3. Fixed Request Encoding Bug

  • Updated encode_chat_body to use maybe_put for conditional parameters
  • Prevents null values from being included in API requests
  • Ensures translated parameters are properly applied

Key Changes

  • lib/req_llm/provider.ex: Added translate_options/3 callback
  • lib/req_llm/provider/dsl.ex: Added translation helper functions
  • lib/req_llm/generation.ex: Added on_unsupported option and translation integration
  • lib/req_llm/providers/openai.ex: Implemented o1 model translation and fixed encoding
  • Comprehensive tests: Unit tests and integration tests for all translation functionality

Documentation Updates

  • AGENTS.md: Added options translation to provider architecture documentation
  • usage-rules.md: Added guidance on provider-specific parameter handling
  • README.md: Added automatic parameter translation to features list

Verification

Real API Testing: Confirmed fix works with actual OpenAI API calls
All Tests Pass: No regressions, all existing functionality preserved
Demo Script: Included working demonstration of the fix
Backward Compatibility: Standard models continue to work unchanged

Usage Example

# This now works automatically for o1 models
{:ok, response} = ReqLLM.generate_text(
  "openai:o1-mini", 
  "Hello", 
  max_tokens: 150,        # Automatically becomes max_completion_tokens
  temperature: 0.7        # Automatically dropped with warning
)

Resolves #8

mikehostetler and others added 4 commits September 15, 2025 08:34
- Introduced `translate_options/3` callback in the `ReqLLM.Provider` module to allow providers to modify option keys and values before API calls, addressing parameter name differences and model-specific restrictions.
- Added `on_unsupported` option in the schema to manage unsupported parameter translations with configurable behaviors (warn, error, ignore).
- Enhanced the `ReqLLM.Generation` module to utilize the new translation capabilities, ensuring proper handling of options during request preparation.
- Implemented translation helper functions in the `ReqLLM.Provider.DSL` module for renaming and dropping options, along with combining warnings.
- Added comprehensive test coverage for translation functionality in both the `OpenAI` provider and the generation pipeline, ensuring robust validation of option handling.
- Updated AGENTS.md to include the `translate_options/3` callback, detailing its role in handling model-specific parameter requirements.
- Added a section in usage-rules.md explaining the automatic parameter translation feature, with examples demonstrating its implementation.
- Revised README.md to highlight the automatic parameter translation for model-specific requirements, improving clarity for users.
- Add translate_options/3 callback to Provider behavior for model-specific parameter translation
- Implement OpenAI o1 model parameter translation: max_tokens → max_completion_tokens and drop temperature
- Fix encode_chat_body to use maybe_put for conditional parameters instead of hardcoding
- Add comprehensive tests for translation functionality and integration
- Update documentation to reflect new options translation capability
- Add demo script proving Issue #8 resolution

Resolves #8: OpenAI o1 models now work with standard ReqLLM parameters through automatic translation

Amp-Thread-ID: https://ampcode.com/threads/T-f9ddd7ac-ace0-4db3-86fb-9aa00e33592a
Co-authored-by: Amp <amp@ampcode.com>
@coveralls

coveralls commented Sep 15, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 56683a4cb50c99c9f38265cfa4419f20280ad9f8-PR-11

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 2 of 34 (5.88%) changed or added relevant lines in 2 files are covered.
  • 269 unchanged lines in 9 files lost coverage.
  • Overall coverage increased (+0.9%) to 22.826%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/req_llm/providers/openai.ex 2 16 12.5%
lib/req_llm/generation.ex 0 18 0.0%
Files with Coverage Reduction New Missed Lines %
lib/req_llm.ex 17 6.45%
lib/req_llm/generation.ex 19 8.33%
lib/req_llm/providers/openai.ex 22 4.23%
lib/mix/tasks/stream_text.ex 25 0.0%
lib/req_llm/providers/google.ex 30 0.0%
lib/req_llm/providers/groq.ex 36 0.0%
lib/req_llm/providers/x_ai.ex 36 0.0%
lib/req_llm/providers/anthropic.ex 42 0.0%
lib/req_llm/providers/openrouter.ex 42 0.0%
Totals Coverage Status
Change from base Build fad21d4ab875c379a3ec366e984610fde1d72512: 0.9%
Covered Lines: 525
Relevant Lines: 2300

💛 - Coveralls

Copilot AI left a comment

Copy link
Copy Markdown

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 implements automatic parameter translation for OpenAI o1 models to resolve compatibility issues with ReqLLM's standard API parameters. The changes add a provider-level translation system that allows automatic conversion of standard parameters (like max_tokens) to model-specific requirements (like max_completion_tokens for o1 models).

  • Added provider options translation system with translate_options/3 callback
  • Implemented OpenAI o1 model parameter translation with warning system
  • Fixed request encoding to properly handle translated parameters

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/req_llm/provider.ex Added optional translate_options/3 callback to Provider behavior
lib/req_llm/provider/dsl.ex Added translation helper functions to DSL for providers
lib/req_llm/generation.ex Integrated translation pipeline and added on_unsupported option
lib/req_llm/providers/openai.ex Implemented o1 model translation and fixed encoding with maybe_put
test/req_llm/providers/openai_test.exs Comprehensive unit tests for OpenAI translation functionality
test/req_llm/provider_dsl_translation_test.exs Tests for DSL translation helper functions
test/req_llm/generation_translation_test.exs Integration tests for translation pipeline
usage-rules.md Added documentation for provider-specific parameter handling
README.md Added automatic parameter translation to features list
AGENTS.md Updated provider architecture documentation
Comments suppressed due to low confidence (1)

lib/req_llm/providers/openai.ex:15

  • [nitpick] Inconsistent trailing whitespace removed from line 15 but not from other comment lines in the same block. Consider reviewing lines 17 and maintaining consistent formatting.
  # Option 1: Set directly in JidoKeys

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread lib/req_llm/providers/openai.ex Outdated
Comment on lines +229 to +235
%{
model: request.options[:model] || request.options[:id],
temperature: request.options[:temperature],
max_tokens: request.options[:max_tokens],
stream: request.options[:stream]
}
|> maybe_put(:temperature, request.options[:temperature])
|> maybe_put(:max_tokens, request.options[:max_tokens])
|> maybe_put(:max_completion_tokens, request.options[:max_completion_tokens])

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

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

The encoding now handles both :max_tokens and :max_completion_tokens parameters, but this could lead to conflicts if both are present after translation. Consider adding validation to ensure only one is used, or document the precedence behavior.

Copilot uses AI. Check for mistakes.
Comment thread lib/req_llm/generation.ex
:ok

:error ->
if warnings == [], do: :ok, else: {:error, {:unsupported_options, warnings}}

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

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

The error tuple {:error, {:unsupported_options, warnings}} may not provide sufficient context for debugging. Consider using a more descriptive error structure or adding the original options to the error message.

Suggested change
if warnings == [], do: :ok, else: {:error, {:unsupported_options, warnings}}
if warnings == [], do: :ok, else: {:error, {:unsupported_options, warnings, opts}}

Copilot uses AI. Check for mistakes.
- Implemented `translate_options/3` for OpenAI o3 models to rename `max_tokens` to `max_completion_tokens` and drop the unsupported `temperature` parameter.
- This enhancement ensures compatibility with OpenAI o3 model requirements, improving the overall functionality of the provider.

Resolves #8: OpenAI o3 models now correctly handle standard ReqLLM parameters through automatic translation.
- Introduced a new validation function `validate_mutex!/3` in the `ReqLLM.Provider.DSL` module to ensure that conflicting parameters are not used together, improving error handling for provider options.
- Updated the OpenAI provider to dynamically determine the appropriate token parameter (`max_tokens` or `max_completion_tokens`) based on the model name, enhancing compatibility with different OpenAI models.

This change improves the robustness of parameter handling and aligns with the recent enhancements for model-specific option translation.
@mikehostetler mikehostetler merged commit e8f08de into main Sep 15, 2025
1 of 2 checks passed
@mikehostetler mikehostetler deleted the feature/unique-model-provider-options branch September 15, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: O1 and O3 models from openai only accepts max_completion_tokens instead of max_tokens

3 participants