Empty args/inputs when turning streaming on and setting tool choice to any
Problem
Description When calling the Anthropic client with `streaming=True` and `tool_choice={"type": "any"/"tool" }` the output returns a tool call but with empty args. This is problematic for a few other reasons beyond no args being returned. For example, quite a few packages rely on the `anthropic-sdk`, one of which is `langchain-anthropic` (ref). Expected response I would expect that the output includes the inputs/args required for the tool call when streaming. Reproduction steps I've added a notebook to highlight some things: https://gist.github.com/kwnath/f42737c023767d5effdcca20cb5bd0a6
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Empty Args in Tool Calls with Streaming Enabled
The issue arises because the Anthropic client does not properly serialize the input arguments when the streaming option is enabled and the tool choice is set to 'any' or a specific tool. This leads to the output containing a tool call without the necessary arguments, which disrupts downstream processing in dependent packages like langchain-anthropic.
Awaiting Verification
Be the first to verify this fix
- 1
Update Anthropic Client Configuration
Ensure that the Anthropic client is correctly configured to handle streaming inputs. This may involve modifying the client initialization parameters to explicitly define input arguments when streaming is enabled.
pythonclient = AnthropicClient(streaming=True, tool_choice={'type': 'any', 'args': {'required_arg': 'value'}}) - 2
Modify Tool Call Structure
Adjust the structure of the tool call to ensure that it includes the necessary arguments. This may require checking the implementation of the tool call method within the Anthropic SDK to ensure that it captures and returns the arguments correctly when streaming is active.
pythondef call_tool(tool_name, args): if not args: raise ValueError('Arguments cannot be empty when streaming') return tool_execute(tool_name, args) - 3
Implement Input Validation
Add input validation checks before making the tool call to ensure that the required arguments are present. This will prevent empty args from being sent to the tool and provide clearer error messages if inputs are missing.
pythondef validate_inputs(args): if not args or 'required_arg' not in args: raise ValueError('Missing required arguments') - 4
Test with Sample Inputs
Create test cases that simulate the streaming scenario with various tool choices and inputs. This will help confirm that the tool calls are being made with the correct arguments and that the output is as expected.
pythondef test_tool_call(): try: response = call_tool('example_tool', {'required_arg': 'test'}) assert response is not None except ValueError as e: print(f'Test failed: {e}') - 5
Update Documentation
Ensure that the documentation for the Anthropic client reflects the changes made to handle streaming inputs and tool calls. This will help users understand how to properly configure their calls and avoid similar issues in the future.
Validation
Confirm that the output from the Anthropic client includes the necessary arguments in the tool call when streaming is enabled. Run the provided test cases and ensure they pass without raising errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep