@@ -2458,15 +2458,16 @@ def _get_llm_for_structured_output_when_thinking_is_enabled(
24582458 formatted_tool : AnthropicTool ,
24592459 ) -> Runnable [LanguageModelInput , BaseMessage ]:
24602460 thinking_admonition = (
2461- "Anthropic structured output relies on forced tool calling, "
2462- "which is not supported when `thinking` is enabled. This method will raise "
2463- "langchain_core.exceptions.OutputParserException if tool calls are not "
2464- "generated. Consider disabling `thinking` or adjust your prompt to ensure "
2465- "the tool is called."
2461+ "You are attempting to use structured output via forced tool calling, "
2462+ "which is not guaranteed when `thinking` is enabled. This method will "
2463+ "raise an OutputParserException if tool calls are not generated. Consider "
2464+ "disabling `thinking` or adjust your prompt to ensure the tool is called."
24662465 )
24672466 warnings .warn (thinking_admonition , stacklevel = 2 )
24682467 llm = self .bind_tools (
24692468 [schema ],
2469+ # We don't specify tool_choice here since the API will reject attempts to
2470+ # force tool calls when thinking=true
24702471 ls_structured_output_format = {
24712472 "kwargs" : {"method" : "function_calling" },
24722473 "schema" : formatted_tool ,
@@ -2998,7 +2999,7 @@ class AnswerWithJustification(BaseModel):
29982999 else :
29993000 llm = self .bind_tools (
30003001 [schema ],
3001- tool_choice = tool_name ,
3002+ tool_choice = tool_name , # Force tool call
30023003 ls_structured_output_format = {
30033004 "kwargs" : {"method" : "function_calling" },
30043005 "schema" : formatted_tool ,
0 commit comments