Skip to content

Commit 5bd0014

Browse files
committed
fix-extra-fields-inputschema
Signed-off-by: yxia216 <[email protected]>
1 parent 96a2fdd commit 5bd0014

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

internal/translator/openai_gcpanthropic.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ func translateOpenAItoAnthropicTools(openAITools []openai.Tool, openAIToolChoice
199199
inputSchema.Required = requiredSlice
200200
}
201201

202+
// Keys to skip
203+
keysToSkip := map[string]bool{
204+
"required": true,
205+
"type": true,
206+
"properties": true,
207+
}
208+
209+
// ExtraFieldsMap to construct
210+
ExtraFieldsMap := make(map[string]any)
211+
212+
// Iterate over the original map
213+
for key, value := range paramsMap {
214+
// Check if the current key should be skipped
215+
if _, found := keysToSkip[key]; found {
216+
continue // Skip the current iteration
217+
}
218+
219+
// If not skipped, add the key-value pair to the new map
220+
ExtraFieldsMap[key] = value
221+
}
222+
inputSchema.ExtraFields = ExtraFieldsMap
223+
202224
toolParam.InputSchema = inputSchema
203225
}
204226

internal/translator/openai_gcpanthropic_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,77 @@ func TestTranslateOpenAItoAnthropicTools(t *testing.T) {
12651265
},
12661266
expectErr: true,
12671267
},
1268+
{
1269+
name: "nested schema in tool's defintions",
1270+
openAIReq: &openai.ChatCompletionRequest{
1271+
Tools: []openai.Tool{
1272+
{
1273+
Type: "function",
1274+
Function: &openai.FunctionDefinition{
1275+
Name: "get_weather",
1276+
Description: "Get the weather without type",
1277+
Parameters: map[string]any{
1278+
"properties": map[string]any{
1279+
"location": map[string]any{"type": "string"},
1280+
},
1281+
"required": []any{"location"},
1282+
"$defs": map[string]any{
1283+
"ReferencePassage": map[string]any{
1284+
"properties": map[string]any{
1285+
"url": map[string]any{
1286+
"title": "Url",
1287+
"type": "string",
1288+
},
1289+
"passage_id": map[string]any{
1290+
"title": "Passage Id",
1291+
"type": "string",
1292+
},
1293+
},
1294+
"required": []string{"url", "passage_id"},
1295+
"title": "ReferencePassage",
1296+
"type": "object",
1297+
},
1298+
},
1299+
},
1300+
},
1301+
},
1302+
},
1303+
},
1304+
expectedTools: []anthropic.ToolUnionParam{
1305+
{
1306+
OfTool: &anthropic.ToolParam{
1307+
Name: "get_weather",
1308+
Description: anthropic.String("Get the weather without type"),
1309+
InputSchema: anthropic.ToolInputSchemaParam{
1310+
Type: "",
1311+
Properties: map[string]any{
1312+
"location": map[string]any{"type": "string"},
1313+
},
1314+
Required: []string{"location"},
1315+
ExtraFields: map[string]any{
1316+
"$defs": map[string]any{
1317+
"ReferencePassage": map[string]any{
1318+
"properties": map[string]any{
1319+
"url": map[string]any{
1320+
"title": "Url",
1321+
"type": "string",
1322+
},
1323+
"passage_id": map[string]any{
1324+
"title": "Passage Id",
1325+
"type": "string",
1326+
},
1327+
},
1328+
"required": []string{"url", "passage_id"},
1329+
"title": "ReferencePassage",
1330+
"type": "object",
1331+
},
1332+
},
1333+
},
1334+
},
1335+
},
1336+
},
1337+
},
1338+
},
12681339
}
12691340

12701341
for _, tt := range tests {

0 commit comments

Comments
 (0)