Skip to content

Commit b798d5e

Browse files
committed
Remove unsupported formats from schemas in IChatClient implementation
1 parent 93f36a2 commit b798d5e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ where p is not null
4949
{
5050
Name = f.Name,
5151
Description = f.Description,
52-
Parameters = ParseFunctionParameters(f.JsonSchema),
52+
Parameters = ParseFunctionParameters(s_schemaTransformerCache.GetOrCreateTransformedSchema(f)),
5353
}
5454
).ToList();
5555

@@ -598,4 +598,29 @@ public static IList<AIContent> ToAiContents(this List<Part>? parts)
598598
.SelectMany(s => s.Contents).OfType<FunctionCallContent>().ToList();
599599
return aiFunction.Count > 0 ? aiFunction : null;
600600
}
601+
602+
/// <summary>
603+
/// Gets a JSON schema transformer cache conforming to known Gemini restrictions.
604+
/// </summary>
605+
private static AIJsonSchemaTransformCache s_schemaTransformerCache { get; } = new(new()
606+
{
607+
TransformSchemaNode = (ctx, node) =>
608+
{
609+
// Move content from common but unsupported properties to description. In particular, we focus on properties that
610+
// the AIJsonUtilities schema generator might produce and that we know to be unsupported by Gemini.
611+
612+
if (node is JsonObject schemaObj &&
613+
schemaObj["format"] is { } formatNode &&
614+
(formatNode.GetValueKind() is not JsonValueKind.String || formatNode.GetValue<string>() is not ("enum" or "date-time")))
615+
{
616+
_ = schemaObj.Remove("format");
617+
618+
schemaObj["description"] = schemaObj["description"] is { } descriptionNode && descriptionNode.GetValueKind() is JsonValueKind.String ?
619+
$"{descriptionNode.GetValue<string>()}\n{formatNode}" :
620+
formatNode.ToString();
621+
}
622+
623+
return node;
624+
},
625+
});
601626
}

0 commit comments

Comments
 (0)