From cfba4394174cd6d22050d9b6898ddb039908a835 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 28 Jan 2026 07:02:05 +0000 Subject: [PATCH] Fix tool_choice reset for structured output sampling Preserve tool_choice='required' when result_type is set to ensure LLM calls final_response instead of returning text responses. Fixes #3011 Co-authored-by: Bill Easton --- src/fastmcp/server/sampling/run.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/fastmcp/server/sampling/run.py b/src/fastmcp/server/sampling/run.py index f5c73afef..729968916 100644 --- a/src/fastmcp/server/sampling/run.py +++ b/src/fastmcp/server/sampling/run.py @@ -634,7 +634,8 @@ async def sample_impl( # Continue with the updated history current_messages = step.history - # After first iteration, reset tool_choice to auto - tool_choice = None + # After first iteration, reset tool_choice to auto (unless structured output is required) + if result_type is None or result_type is str: + tool_choice = None raise RuntimeError(f"Sampling exceeded maximum iterations ({max_iterations})")