Skip to content

fix: forward max_tokens in Chat Completions API requests#1495

Merged
joshua-mo-143 merged 1 commit into0xPlaygrounds:mainfrom
sonicxml:fix/chat-completions-max-tokens
Mar 10, 2026
Merged

fix: forward max_tokens in Chat Completions API requests#1495
joshua-mo-143 merged 1 commit into0xPlaygrounds:mainfrom
sonicxml:fix/chat-completions-max-tokens

Conversation

@sonicxml
Copy link
Contributor

@sonicxml sonicxml commented Mar 9, 2026

Summary

  • The Chat Completions provider's CompletionRequest struct is missing a max_tokens field. When .max_tokens(N) is called on the agent builder, the value flows into the core CompletionRequest but is silently discarded during TryFrom<OpenAIRequestParams> conversion — the destructuring uses .. which drops it, and the provider struct has no field to receive it.
  • The Responses API provider handles this correctly via max_output_tokens, so only the Chat Completions path is affected.
  • Adds max_tokens: Option<u64> to the provider's CompletionRequest, extracts it during conversion, and serializes it in the API request body.

Test plan

  • Added test_max_tokens_is_forwarded_to_request — verifies max_tokens appears in serialized JSON when set
  • Added test_max_tokens_omitted_when_none — verifies max_tokens is omitted from JSON when None
  • All existing providers::openai::completion::tests pass (7/7)

The Chat Completions provider's `CompletionRequest` struct was missing
the `max_tokens` field. When users set `.max_tokens(N)` on the agent
builder, the value was accepted and stored in the core
`CompletionRequest` but silently discarded during conversion to the
OpenAI Chat Completions request — the `TryFrom` destructured with `..`
which dropped it, and the struct had no field to receive it.

The Responses API provider correctly handles this via
`max_output_tokens`, so only the Chat Completions path was affected.

The fix adds `max_tokens: Option<u64>` to the provider's
`CompletionRequest`, extracts it from the core request during
conversion, and includes it in the serialized API request.
Copy link
Collaborator

@joshua-mo-143 joshua-mo-143 left a comment

Choose a reason for hiding this comment

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

lgtm, thank you!

@joshua-mo-143 joshua-mo-143 added this pull request to the merge queue Mar 10, 2026
Merged via the queue into 0xPlaygrounds:main with commit 1fd1a92 Mar 10, 2026
6 checks passed
@github-actions github-actions bot mentioned this pull request Mar 9, 2026
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.

2 participants