Skip to content

fix(py/plugins)!: OpenAI tool_choice after tool turns, integer topK, evaluator typing - #6696

Merged
huangjeff5 merged 8 commits into
mainfrom
fix/py-small-preexisting-fixes
Oct 29, 2026
Merged

huangjeff5 merged 8 commits into
mainfrom
fix/py-small-preexisting-fixes

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Three small fixes for plugin bugs on main: OpenAI keeps the caller's tool_choice after a tool turn; Gemini and Anthropic send topK as an integer on the wire; and Vertex evaluator metric lookup type-checks under pyrefly on Python 3.10.

from genkit_google_genai import GeminiConfig

# 1. Ask for top-k sampling
config = GeminiConfig(top_k=40)

# 2. The generateContent wire body
# --- BEFORE ---
# => {"generationConfig": {"topK": 40.0}}
# --- AFTER ---
# => {"generationConfig": {"topK": 40}}
# 1. Chain two tools on OpenAI
response = await ai.generate(
    model='openai/gpt-4o',
    prompt='Is the pho in stock, and is it nut-free?',
    tools=['lookup_menu', 'check_allergens'],
)

# 2. The second chat.completions body, after lookup_menu returned
# --- BEFORE ---
# => {"tool_choice": "none", ...}   the model can't call check_allergens
# --- AFTER ---
# => no tool_choice override, so the model chains check_allergens

Decisions

  • The OpenAI model previously forced tool_choice='none' once history held any tool message, so after one tool result the model could not call a second tool, 'required' was dropped from the second turn on, and an old tool turn (role='tool') in history when request.tools was empty sent tool_choice='none' without tools (which OpenAI rejects with 400). Now, with no tool_choice set, the model can call tools again after a tool round (bounded by max_turns); 'required' forces a tool call every turn until max_turns stops the loop (matching Anthropic and Go compat_oai); and when request.tools is empty, tool_choice is omitted even if history contains a prior tool round.
  • google-genai 2.21 types GenerateContentConfig.top_k as Optional[float], even though the Gemini API defines topK as int32, turning 40 into 40.0 during GenerateContentConfig(**known) validation. We assign the integer back after constructing the SDK config so it serializes as an int on both Google AI and Vertex AI. Anthropic had the same issue via AnthropicConfig.top_k: float (inherited from ModelConfig); _build_params now converts integral floats to int, matching how it handles max_tokens.
  • pyrefly runs with python_version = "3.10", where StrEnum comes from the strenum backport whose stub types __new__ as returning the base StrEnum instead of the subclass. Looking up VertexAIEvaluationMetricType by iterating members and matching .value preserves the VertexAIEvaluationMetricType subtype without casts or suppressions.

google-genai types GenerateContentConfig.top_k as float, so building the
config coerced 40 to 40.0 and the generateContent body carried
"topK": 40.0 on both GoogleAI and Vertex. The API defines topK as int32.
Reassign the int after construction; the SDK doesn't validate on
assignment.

AnthropicConfig inherits top_k: float from ModelConfig, so the dumped
params sent "top_k": 40.0. Convert integral values to int, as max_tokens
already is.

Other int knobs (maxOutputTokens, candidateCount, seed, logprobs) were
already ints. Ollama coerces through ollama.Options; OpenAI drops top_k.
The OpenAI model forced tool_choice='none' whenever history held a tool
message, whatever the caller asked for. So after one tool result the
model couldn't call another tool: no multi-step chaining, and
tool_choice='required' was dropped from the second turn on. The rule
came in with the DeepSeek plugin (#4051) with no stated reason; it
stopped a 'required' run from re-forcing tool calls forever. Core's
max_turns already bounds that loop, as it does for Anthropic, Gemini
and Go compat_oai, which pass the value through.

Genkit's auto/required/none are OpenAI's values, so they go out
unchanged and None sends nothing. tool_choice now rides only with
tools: OpenAI rejects it without them.

Vertex AI Model Garden's OpenAI-compatible models build the same
OpenAIModel, so they get the fix too.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses type coercion and parameter handling across several Genkit provider packages. Specifically, it ensures that integer sampling parameters (like top_k) are correctly serialized as integers rather than floats for Anthropic and Google GenAI models. It also updates the Django package's __all__ export, refactors Google GenAI's evaluator resolution to support Python 3.10 StrEnum type-checking, and refactors OpenAI's tool_choice logic to allow tool chaining and avoid sending tool_choice when no tools are defined. Relevant unit and integration tests have been added to verify these behaviors. There are no review comments, so I have no feedback to provide.

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM with comments

@huangjeff5 huangjeff5 changed the title fix(py/plugins): OpenAI tool_choice after tool turns, integer topK, evaluator typing, static django __all__ fix(py/plugins): OpenAI tool_choice after tool turns, integer topK, evaluator typing Oct 12, 2026
@huangjeff5
huangjeff5 requested a review from cabljac October 12, 2026 21:39
@huangjeff5 huangjeff5 changed the title fix(py/plugins): OpenAI tool_choice after tool turns, integer topK, evaluator typing fix(py/plugins)!: OpenAI tool_choice after tool turns, integer topK, evaluator typing Oct 12, 2026
@huangjeff5
huangjeff5 merged commit 45ef98a into main Oct 29, 2026
27 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-small-preexisting-fixes branch October 29, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants