feat: add OpenAI-style tool_choice translation in bind_tools()#160
Open
张哲芳 (zhangzhefang-github) wants to merge 1 commit intolangchain-ai:mainfrom
Open
feat: add OpenAI-style tool_choice translation in bind_tools()#160张哲芳 (zhangzhefang-github) wants to merge 1 commit intolangchain-ai:mainfrom
张哲芳 (zhangzhefang-github) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
Add support for translating OpenAI-style tool_choice values to Cohere's format in the bind_tools() method: - "any" -> "REQUIRED" (force tool use) - "none" -> "NONE" (prevent tool use) - "auto" -> removed (default behavior) - Existing Cohere values (REQUIRED/NONE) pass through unchanged This allows users to use OpenAI-compatible tool_choice parameters when working with Cohere models, improving cross-provider compatibility. Added comprehensive unit tests covering all translation cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds support for OpenAI-style
tool_choiceparameter translation in thebind_tools()method, resolving compatibility issues when using Cohere models with cross-provider code.Problem
Currently, when using
create_agent()fromlangchain_v1with Cohere models andresponse_format(which internally usestool_choice="any"), it fails with a 422 error because Cohere expects"REQUIRED"instead of"any".Reference: #161
Solution
Modified
ChatCohere.bind_tools()to automatically translate OpenAI-style tool_choice values:"any"→"REQUIRED"(force tool use)"none"→"NONE"(prevent tool use)"auto"→ removed (default behavior - let model decide)"REQUIRED","NONE") pass through unchangedChanges
bind_tools()method with translation logic and docstringTesting
Added 8 test cases covering:
"any"/"ANY"→"REQUIRED""none"/"NONE"→"NONE""auto"/"AUTO"→ removed"REQUIRED"→ unchangedNone→ no tool_choice parameterBackward Compatibility
✅ Fully backward compatible
Impact
create_agent()withresponse_formatRelated Issues