Update chat_template.jinja to correctly parse types nested in arrays
By forcing recursive calls in render_typescript_type
for "array" types, it now correctly renders any array types that contains items of enum strings or have a "description" field. Before the fix, neither enums nor the "description" field are rendered due to prematurely stopping parsing.
Additionally updated the "any[]" truncation length from 50 to 200, as Enum types typically need longer descriptions.
E.g., these tool schemas are now correctly rendered:
sample['tools'] = \
[{'function': {'description': 'tool1 does XYZ',
'name': 'tool1',
'parameters': {'properties': {'enum_array': {'description': 'Array of enums',
'items': {'description': 'One of these enums',
'enum': ['TypeA', 'TypeB'],
'type': 'string'},
'type': 'array'}},
'required': ['enum_array'],
'type': 'object'}},
'type': 'function'}]
When running templated_output = tokenizer.apply_chat_template(sample['messages'], tools=sample['tools'])
and decoding with tokenizer.decode(templated_output['input_ids'], skip_special_tokens=False)
,
Before this change, it omits enum definitions:
# Tools
## functions
namespace functions {
// tool1 does XYZ
type tool1 = (_: {
// Array of enums
enum_array: string[],
}) => any;
} // namespace functions
After this change, it prints:
# Tools
## functions
namespace functions {
// tool1 does XYZ
type tool1 = (_: {
// Array of enums
enum_array: "TypeA" | "TypeB"[],
}) => any;
} // namespace functions
Here, the line enum_array: "TypeA" | "TypeB"[],
is the expected behavior.