Repository navigation
fix(py): OpenAI tool_choice after tool turns, integer topK, evaluator typing, static django __all__ - #6692
fix(py): OpenAI tool_choice after tool turns, integer topK, evaluator typing, static django __all__#6692huangjeff5 wants to merge 4 commits into
Conversation
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.
There was a problem hiding this comment.
Code Review
This pull request ensures that integer configuration parameters, specifically top_k, are correctly transmitted as integers rather than being coerced to floats by the underlying SDKs for Anthropic and Gemini models. It also resolves a Python 3.10 type-stub issue with StrEnum in the Google GenAI evaluator and cleans up the __all__ exports in the Django package. Feedback on the changes suggests making the top_k extraction in the Gemini model more robust by also handling float values that represent integers, aligning it with the Anthropic implementation.
| if isinstance(known.get('top_k'), int): | ||
| cfg.top_k = known['top_k'] |
There was a problem hiding this comment.
To make the top_k extraction more robust and consistent with the Anthropic implementation, consider handling both int (excluding bool) and float values that represent integers (e.g., 40.0 passed via a raw dictionary config). This ensures that any integral float is correctly coerced to an integer before being assigned to cfg.top_k.
| if isinstance(known.get('top_k'), int): | |
| cfg.top_k = known['top_k'] | |
| top_k = known.get('top_k') | |
| if isinstance(top_k, int) and not isinstance(top_k, bool): | |
| cfg.top_k = top_k | |
| elif isinstance(top_k, float) and top_k.is_integer(): | |
| cfg.top_k = int(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 (genkit-ai#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.
|
Moved to #6696 so the branch lives in genkit-ai/genkit. |
Four small fixes for bugs that already exist on main: OpenAI honors
tool_choiceafter a tool turn; Gemini and Anthropic now sendtopKas an integer; the Vertex evaluator lookup type-checks under pyrefly; andgenkit_django.__all__is a static list.Decisions
tool_choiceon every turn: the OpenAI model forcedtool_choice='none'once history held any tool message, so after one tool result the model couldn't call another, and'required'was dropped from the second turn on. The rule came in with the DeepSeek plugin (feat(py): Implemented deepseek plugin #4051) with no stated reason; it stopped a'required'run from re-forcing tool calls forever. Core'smax_turnsalready bounds that loop, as it does for Anthropic, Gemini and Go compat_oai, so we passauto/required/nonethrough unchanged.tool_choicenow rides only with tools, because OpenAI rejects it without them. Vertex Model Garden's OpenAI-compatible models share this path.GenerateContentConfig.top_kasOptional[float], though the Gemini API definestopKas int32. SoGenerateContentConfig(**known)turned40into40.0. Genkit's ownGeminiConfig.top_kwas alreadyint. We assign the int back after building the SDK config. The SDK doesn't validate on assignment, so the int survives to the wire, on Google AI and Vertex. Anthropic had the same bug throughAnthropicConfig.top_k: float, inherited fromGenerationCommonConfig._build_paramsnow sends an integral float as int, the same way it already treatsmax_tokens. Vertex Model Garden Claude shares that path.python_version = "3.10", soStrEnumcomes from thestrenumbackport. Its stub types__new__as returning the baseStrEnum, which madeVertexAIEvaluationMetricType(name.upper())aStrEnum. That didn't matchcreate_vertex_evaluators(metrics: list[VertexAIEvaluationMetricType]). We find the member by iterating the enum, which yields the subclass type, so there's no cast or ignore, and matching is still by value. Behavior is unchanged.__all__:genkit_django.__all__built an entry fromgenkit_django_handler.__name__. pyright can't analyze that, and it was the only warning inpyright packages/*/src. The list is now literal; the runtime value is identical.Verification
openai_tool_choice_test.py: HTTP bodies for each of the 4 values with tools,'required'andNoneacross a two-tool chain, andtool_choicedropped with no tools. 5 of 9 fail without the fix.gemini_int_config_wire_test.py: Google AI and Vertex × typed and dict config. Assertstype(...) is intfortopK,maxOutputTokens,candidateCount,seedandlogprobsin the real HTTP body.top_kbody test.packages/*/src(0 errors, 0 warnings), ty,generate_schema_typing --cianduv lock --check: all clean. pytest: 5225 passed.