@@ -700,22 +700,26 @@ def check_tool_usage(cls, data):
700
700
701
701
# ensure that if "tool_choice" is specified as an object,
702
702
# it matches a valid tool
703
+ correct_usage_message = 'Correct usage: `{"type": "function",' \
704
+ ' "function": {"name": "my_function"}}`'
703
705
if isinstance (data ["tool_choice" ], dict ):
704
706
valid_tool = False
705
- specified_function = data ["tool_choice" ].get ("function" )
706
- if not specified_function :
707
+ function = data ["tool_choice" ].get ("function" )
708
+ if not isinstance ( function , dict ) :
707
709
raise ValueError (
708
- "Expected field `function` in `tool_choice`."
709
- " Correct usage: `{\" type\" : \" function\" ,"
710
- " \" function\" : {\" name\" : \" my_function\" }}`" )
711
- specified_function_name = specified_function .get ("name" )
712
- if not specified_function_name :
710
+ f"Invalid value for `function`: `{ function } ` in "
711
+ f"`tool_choice`! { correct_usage_message } " )
712
+ if "name" not in function :
713
+ raise ValueError (f"Expected field `name` in `function` in "
714
+ f"`tool_choice`! { correct_usage_message } " )
715
+ function_name = function ["name" ]
716
+ if not isinstance (function_name ,
717
+ str ) or len (function_name ) == 0 :
713
718
raise ValueError (
714
- "Expected field `name` in `function` in `tool_choice`."
715
- "Correct usage: `{\" type\" : \" function\" , "
716
- "\" function\" : {\" name\" : \" my_function\" }}`" )
719
+ f"Invalid `name` in `function`: `{ function_name } `"
720
+ f" in `tool_choice`! { correct_usage_message } " )
717
721
for tool in data ["tools" ]:
718
- if tool ["function" ]["name" ] == specified_function_name :
722
+ if tool ["function" ]["name" ] == function_name :
719
723
valid_tool = True
720
724
break
721
725
if not valid_tool :
0 commit comments