@@ -1022,10 +1022,26 @@ def test_anthropic_json_mode_and_tool_call_response(
1022
1022
[
1023
1023
("stop" , ["stop" ], True ), # basic string
1024
1024
(["stop1" , "stop2" ], ["stop1" , "stop2" ], True ), # list of strings
1025
- (" " , None , True ), # whitespace string should be dropped when drop_params is True
1026
- (" " , [" " ], False ), # whitespace string should be kept when drop_params is False
1027
- (["stop1" , " " , "stop2" ], ["stop1" , "stop2" ], True ), # list with whitespace that should be filtered
1028
- (["stop1" , " " , "stop2" ], ["stop1" , " " , "stop2" ], False ), # list with whitespace that should be kept
1025
+ (
1026
+ " " ,
1027
+ None ,
1028
+ True ,
1029
+ ), # whitespace string should be dropped when drop_params is True
1030
+ (
1031
+ " " ,
1032
+ [" " ],
1033
+ False ,
1034
+ ), # whitespace string should be kept when drop_params is False
1035
+ (
1036
+ ["stop1" , " " , "stop2" ],
1037
+ ["stop1" , "stop2" ],
1038
+ True ,
1039
+ ), # list with whitespace that should be filtered
1040
+ (
1041
+ ["stop1" , " " , "stop2" ],
1042
+ ["stop1" , " " , "stop2" ],
1043
+ False ,
1044
+ ), # list with whitespace that should be kept
1029
1045
(None , None , True ), # None input
1030
1046
],
1031
1047
)
@@ -1035,3 +1051,34 @@ def test_map_stop_sequences(stop_input, expected_output, drop_params):
1035
1051
config = AnthropicConfig ()
1036
1052
result = config ._map_stop_sequences (stop_input )
1037
1053
assert result == expected_output
1054
+
1055
+
1056
+ @pytest .mark .asyncio
1057
+ async def test_anthropic_structured_output ():
1058
+ """
1059
+ Test the _transform_response_for_structured_output
1060
+
1061
+ Relevant Issue: https://github.com/BerriAI/litellm/issues/8291
1062
+ """
1063
+ from litellm import acompletion
1064
+
1065
+ args = {
1066
+ "model" : "claude-3-5-sonnet-20240620" ,
1067
+ "seed" : 3015206306868917280 ,
1068
+ "stop" : None ,
1069
+ "messages" : [
1070
+ {
1071
+ "role" : "system" ,
1072
+ "content" : 'You are a hello world agent.\n Always respond in the following valid JSON format: {\n "response": "response",\n }\n ' ,
1073
+ },
1074
+ {"role" : "user" , "content" : "Respond with hello world" },
1075
+ ],
1076
+ "temperature" : 0 ,
1077
+ "response_format" : {"type" : "json_object" },
1078
+ "drop_params" : True ,
1079
+ }
1080
+
1081
+ response = await acompletion (** args )
1082
+ assert response is not None
1083
+
1084
+ print (response )
0 commit comments