Skip to content

feat: add OpenAI-style tool_choice translation in bind_tools()#160

Open
张哲芳 (zhangzhefang-github) wants to merge 1 commit intolangchain-ai:mainfrom
zhangzhefang-github:feat/support-tool-choice-any
Open

feat: add OpenAI-style tool_choice translation in bind_tools()#160
张哲芳 (zhangzhefang-github) wants to merge 1 commit intolangchain-ai:mainfrom
zhangzhefang-github:feat/support-tool-choice-any

Conversation

@zhangzhefang-github

Description

This PR adds support for OpenAI-style tool_choice parameter translation in the bind_tools() method, resolving compatibility issues when using Cohere models with cross-provider code.

Problem

Currently, when using create_agent() from langchain_v1 with Cohere models and response_format (which internally uses tool_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)
  • Existing Cohere values ("REQUIRED", "NONE") pass through unchanged
  • Case-insensitive matching

Changes

  1. chat_models.py: Updated bind_tools() method with translation logic and docstring
  2. test_chat_models.py: Added comprehensive parametrized tests covering all translation scenarios

Testing

Added 8 test cases covering:

  • "any" / "ANY""REQUIRED"
  • "none" / "NONE""NONE"
  • "auto" / "AUTO" → removed
  • "REQUIRED" → unchanged
  • None → no tool_choice parameter

Backward Compatibility

Fully backward compatible

  • Existing code using Cohere-style values continues to work unchanged
  • Only adds translation for OpenAI-style values
  • Does not modify any existing behavior

Impact

  • ✅ Unblocks Cohere users from using create_agent() with response_format
  • ✅ Improves cross-provider compatibility
  • ✅ No breaking changes

Related Issues

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_agent() fails with Cohere models when using response_format (hardcoded tool_choice="any")

1 participant