Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - function call with no args when using anthropic api #408

Merged
merged 6 commits into from
Jan 29, 2025

Conversation

ananis25
Copy link
Contributor

When the arguments string is empty, replace it with an empty json dict, so pydantic can parse it correctly.

Fixes #407

Copy link
Owner

@jackmpcollins jackmpcollins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!


Confirmed that Anthropic outputs an empty string in this case with

# Anthropic Raw Response

import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    max_tokens=1024,
    messages=[
        {
          "role": "user",
          "content": "Use the function to generate a random number. It takes no inputs and returns a random number."
        }
    ],
    model="claude-3-5-sonnet-20240620",
    tools=[
        {
          "name": "get_random_number",
          "description": "Get a random number",
          "input_schema": {
            "type": "object",
            "properties": {},
          }
        }
    ],
    # tool_choice={"type": "any"},
) as stream:
  for text in stream:
      print(text, flush=True)

outputting

RawMessageStartEvent(message=Message(id='msg_012K6zWDkhzPoJGpHoXv4EVy', content=[], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason=None, stop_sequence=None, type='message', usage=Usage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=360, output_tokens=5)), type='message_start')
RawContentBlockStartEvent(content_block=TextBlock(text='', type='text'), index=0, type='content_block_start')
RawContentBlockDeltaEvent(delta=TextDelta(text="Certainly! I'll", type='text_delta'), index=0, type='content_block_delta')
TextEvent(type='text', text="Certainly! I'll", snapshot="Certainly! I'll")
RawContentBlockDeltaEvent(delta=TextDelta(text=' use the available function to generate a random number for you. This', type='text_delta'), index=0, type='content_block_delta')
TextEvent(type='text', text=' use the available function to generate a random number for you. This', snapshot="Certainly! I'll use the available function to generate a random number for you. This")
RawContentBlockDeltaEvent(delta=TextDelta(text=" function doesn't require any input parameters, so we can", type='text_delta'), index=0, type='content_block_delta')
TextEvent(type='text', text=" function doesn't require any input parameters, so we can", snapshot="Certainly! I'll use the available function to generate a random number for you. This function doesn't require any input parameters, so we can")
RawContentBlockDeltaEvent(delta=TextDelta(text=' call it directly.', type='text_delta'), index=0, type='content_block_delta')
TextEvent(type='text', text=' call it directly.', snapshot="Certainly! I'll use the available function to generate a random number for you. This function doesn't require any input parameters, so we can call it directly.")
ContentBlockStopEvent(index=0, type='content_block_stop', content_block=TextBlock(text="Certainly! I'll use the available function to generate a random number for you. This function doesn't require any input parameters, so we can call it directly.", type='text'))
RawContentBlockStartEvent(content_block=ToolUseBlock(id='toolu_01B481ftxp9FRP7CRA3EgbPv', input={}, name='get_random_number', type='tool_use'), index=1, type='content_block_start')
RawContentBlockDeltaEvent(delta=InputJSONDelta(partial_json='', type='input_json_delta'), index=1, type='content_block_delta')
InputJsonEvent(type='input_json', partial_json='', snapshot={})
ContentBlockStopEvent(index=1, type='content_block_stop', content_block=ToolUseBlock(id='toolu_01B481ftxp9FRP7CRA3EgbPv', input={}, name='get_random_number', type='tool_use'))
RawMessageDeltaEvent(delta=Delta(stop_reason='tool_use', stop_sequence=None), type='message_delta', usage=MessageDeltaUsage(output_tokens=72))
MessageStopEvent(type='message_stop', message=Message(id='msg_012K6zWDkhzPoJGpHoXv4EVy', content=[TextBlock(text="Certainly! I'll use the available function to generate a random number for you. This function doesn't require any input parameters, so we can call it directly.", type='text'), ToolUseBlock(id='toolu_01B481ftxp9FRP7CRA3EgbPv', input={}, name='get_random_number', type='tool_use')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=Usage(cache_creation_input_tokens=0, cache_read_input_tokens=0, input_tokens=360, output_tokens=72)))

@jackmpcollins jackmpcollins merged commit 65e09df into jackmpcollins:main Jan 29, 2025
1 check passed
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.

Tool calls with no arguments raise parsing errors when using Anthropic chat model
2 participants