Skip to content

Commit 523ebf8

Browse files
author
ochafik
committed
Simplify tool call grammars when there's only 1 tool
1 parent a2fe8a4 commit 523ebf8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

common/tool-call.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,20 @@ llama_tool_call_handler llama_tool_call_handler_init(
448448
{"properties", {
449449
{"tool_calls", {
450450
{"type", "array"},
451-
{"items", json {{"anyOf", tool_call_schemas}}}
451+
{"items", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json {
452+
{"anyOf", tool_call_schemas},
453+
}},
454+
{"minItems", 1},
452455
}},
453456
}},
454457
{"required", json::array({"tool_calls"})},
455458
}
456459
: json {
457460
{"type", "object"},
458461
{"properties", {
459-
{"tool_call", json {{"anyOf", tool_call_schemas}}},
462+
{"tool_call", tool_call_schemas.size() == 1 ? tool_call_schemas[0] : json {
463+
{"anyOf", tool_call_schemas},
464+
}},
460465
}},
461466
{"required", json::array({"tool_call"})},
462467
};
@@ -473,6 +478,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
473478
: json_schema
474479
},
475480
}},
481+
{"required", json::array({"response"})},
476482
},
477483
})}
478484
}
@@ -514,7 +520,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
514520
}
515521
auto schema = json {
516522
{"type", "array"},
517-
{"items", json {{"anyOf", schemas}}},
523+
{"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}},
518524
{"minItems", 1},
519525
};
520526
if (!parallel) {
@@ -548,7 +554,7 @@ llama_tool_call_handler llama_tool_call_handler_init(
548554
}
549555
auto schema = json {
550556
{"type", "array"},
551-
{"items", json {{"anyOf", schemas}}},
557+
{"items", schemas.size() == 1 ? schemas[0] : json {{"anyOf", schemas}}},
552558
{"minItems", 1},
553559
};
554560
if (!parallel) {

0 commit comments

Comments
 (0)