Skip to content

Commit a97ea95

Browse files
committed
fix-extra-fields-inputschema
Signed-off-by: yxia216 <yxia216@bloomberg.net>
1 parent c092761 commit a97ea95

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

internal/extproc/translator/openai_gcpanthropic.go

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

187+
// Keys to skip
188+
keysToSkip := map[string]bool{
189+
"required": true,
190+
"type": true,
191+
"properties": true,
192+
}
193+
194+
// ExtraFieldsMap to construct
195+
ExtraFieldsMap := make(map[string]any)
196+
197+
// Iterate over the original map
198+
for key, value := range paramsMap {
199+
// Check if the current key should be skipped
200+
if _, found := keysToSkip[key]; found {
201+
continue // Skip the current iteration
202+
}
203+
204+
// If not skipped, add the key-value pair to the new map
205+
ExtraFieldsMap[key] = value
206+
}
207+
inputSchema.ExtraFields = ExtraFieldsMap
208+
187209
toolParam.InputSchema = inputSchema
188210
}
189211

internal/extproc/translator/openai_gcpanthropic_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,77 @@ func TestTranslateOpenAItoAnthropicTools(t *testing.T) {
12481248
},
12491249
expectErr: true,
12501250
},
1251+
{
1252+
name: "nested schema in tool's defintions",
1253+
openAIReq: &openai.ChatCompletionRequest{
1254+
Tools: []openai.Tool{
1255+
{
1256+
Type: "function",
1257+
Function: &openai.FunctionDefinition{
1258+
Name: "get_weather",
1259+
Description: "Get the weather without type",
1260+
Parameters: map[string]any{
1261+
"properties": map[string]any{
1262+
"location": map[string]any{"type": "string"},
1263+
},
1264+
"required": []any{"location"},
1265+
"$defs": map[string]any{
1266+
"ReferencePassage": map[string]any{
1267+
"properties": map[string]any{
1268+
"url": map[string]any{
1269+
"title": "Url",
1270+
"type": "string",
1271+
},
1272+
"passage_id": map[string]any{
1273+
"title": "Passage Id",
1274+
"type": "string",
1275+
},
1276+
},
1277+
"required": []string{"url", "passage_id"},
1278+
"title": "ReferencePassage",
1279+
"type": "object",
1280+
},
1281+
},
1282+
},
1283+
},
1284+
},
1285+
},
1286+
},
1287+
expectedTools: []anthropic.ToolUnionParam{
1288+
{
1289+
OfTool: &anthropic.ToolParam{
1290+
Name: "get_weather",
1291+
Description: anthropic.String("Get the weather without type"),
1292+
InputSchema: anthropic.ToolInputSchemaParam{
1293+
Type: "",
1294+
Properties: map[string]any{
1295+
"location": map[string]any{"type": "string"},
1296+
},
1297+
Required: []string{"location"},
1298+
ExtraFields: map[string]any{
1299+
"$defs": map[string]any{
1300+
"ReferencePassage": map[string]any{
1301+
"properties": map[string]any{
1302+
"url": map[string]any{
1303+
"title": "Url",
1304+
"type": "string",
1305+
},
1306+
"passage_id": map[string]any{
1307+
"title": "Passage Id",
1308+
"type": "string",
1309+
},
1310+
},
1311+
"required": []string{"url", "passage_id"},
1312+
"title": "ReferencePassage",
1313+
"type": "object",
1314+
},
1315+
},
1316+
},
1317+
},
1318+
},
1319+
},
1320+
},
1321+
},
12511322
}
12521323

12531324
for _, tt := range tests {

0 commit comments

Comments
 (0)