Skip to content

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

@noozo

Description

@noozo

Bug Description

O1 and O3 models from openai only accepts max_completion_tokens instead of max_tokens

Steps to Reproduce

defmodule MyApp.AI do
  @moduledoc """
  High level API for AI calls
  """
  def stream_text(adapter_and_model, prompt, system_prompt, opts \\ []) do
    setup_keys()

    opts = Keyword.put_new(opts, :max_tokens, 1000)

    adapter_and_model
    |> ReqLLM.stream_text(
      [
        ReqLLM.Context.system(system_prompt),
        ReqLLM.Context.user(prompt)
      ],
      opts
    )
    |> Stream.each(&IO.write(&1.text))
    |> Stream.run()
  end

  defp setup_keys do
    ReqLLM.put_key(:openai_api_key, Application.fetch_env!(:my_app, :gpt_api_key))
    ReqLLM.put_key(:anthropic_api_key, Application.fetch_env!(:my_app, :claude_api_key))
    ReqLLM.put_key(:xai_api_key, Application.fetch_env!(:my_app, :grok_api_key))
    ReqLLM.put_key(:google_api_key, Application.fetch_env!(:my_app, :gemini_api_key))
  end
end

MyApp.AI.stream_text("openai:gpt4o", "hello", "Nothing")

Expected Behavior

Expected it to work :P :P :P

Actual Behavior

{:error,
     %ReqLLM.Error.API.Request{
       reason: "Provider response error (400): OpenAI API error",
       status: nil,
       response_body: nil,
       request_body: "{\"messages\":[{\"content\":\"Nothing\",\"role\":\"system\"},{\"content\":\"hello\",\"role\":\"user\"}],\"stream\":true,\"model\":\"o1\",\"max_tokens\":1000,\"temperature\":null}",
       cause: %ReqLLM.Error.API.Response{
         reason: "OpenAI API error",
         response_body: %{
           "error" => %{
             "code" => "unsupported_parameter",
             "message" => "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.",
             "param" => "max_tokens",
             "type" => "invalid_request_error"
           }
         },
         status: 400,
         splode: nil,
         bread_crumbs: [],
         vars: [],
         path: [],
         stacktrace: #Splode.Stacktrace<>,
         class: :api
       },
       splode: nil,
       bread_crumbs: [],
       vars: [],
       path: [],
       stacktrace: #Splode.Stacktrace<>,
       class: :api
     }}

    (elixir 1.18.3) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.18.3) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.18.3) lib/stream.ex:1773: Enumerable.Stream.do_each/4
    (elixir 1.18.3) lib/stream.ex:703: Stream.run/1

Environment

Latest elixir, latest Phoenix, latest req_llm

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions