Skip to content

Default struct values throw in AIFunctionFactory #6379

@stephentoub

Description

@stephentoub

Repro:

using Microsoft.Extensions.AI;

AIFunctionFactory.Create((Guid g = default) => "42");

Expected:
Runs successfully

Actual:

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Guid.
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   at System.Text.Json.JsonSerializer.UnboxOnWrite[T](Object value)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue)
   at System.Text.Json.JsonSerializer.WriteNodeAsObject(Object value, JsonTypeInfo jsonTypeInfo)
   at Microsoft.Extensions.AI.AIJsonUtilities.<>c__DisplayClass30_0.<CreateJsonSchemaCore>g__TransformSchemaNode|0(JsonSchemaExporterContext schemaExporterContext, JsonNode schema)
   at System.Text.Json.Schema.JsonSchema.<ToJsonNode>g__CompleteSchema|100_0(JsonNode schema, <>c__DisplayClass100_0&)
   at System.Text.Json.Schema.JsonSchema.ToJsonNode(JsonSchemaExporterOptions options)
   at System.Text.Json.Schema.JsonSchemaExporter.GetJsonSchemaAsNode(JsonTypeInfo typeInfo, JsonSchemaExporterOptions exporterOptions)
   at Microsoft.Extensions.AI.AIJsonUtilities.CreateJsonSchemaCore(Type type, String parameterName, String description, Boolean hasDefaultValue, Object defaultValue, JsonSerializerOptions serializerOptions, AIJsonSchemaCreateOptions inferenceOptions)
   at Microsoft.Extensions.AI.AIJsonUtilities.CreateFunctionJsonSchema(MethodBase method, String title, String description, JsonSerializerOptions serializerOptions, AIJsonSchemaCreateOptions inferenceOptions)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor..ctor(DescriptorKey key, JsonSerializerOptions serializerOptions)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor.GetOrCreate(MethodInfo method, AIFunctionFactoryOptions options)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunction.Build(MethodInfo method, Object target, AIFunctionFactoryOptions options)
   at Microsoft.Extensions.AI.AIFunctionFactory.Create(Delegate method, String name, String description, JsonSerializerOptions serializerOptions)
   at Program.<Main>$(String[] args)

Metadata

Metadata

Labels

area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions