support for type descriptors $type #5420
Replies: 4 comments
|
Not as a general function-tool argument feature. The repo does use For function tools, I would model the discriminator explicitly in the tool schema, for example |
|
Can you explain a bit more on the exact use-case and why you need to ser-deser ? |
|
For example, I have the following classes: [JsonPolymorphic(TypeDiscriminatorPropertyName = "$type", IgnoreUnrecognizedTypeDiscriminators = false)] public class GeometryCircle : GeometryBase public class GeometryRectangle : GeometryBase public class GeometrySquare : GeometryBase I also have the following function exposed as a tool: List < GeometryBase > GetGeomDetailsById(List ids) The returned list actually contains instances of GeometryCircle, GeometryRectangle, and GeometrySquare. However, when serializing List < GeometryBase > to JSON, the result contains only empty GeometryBase objects, without any of the derived type properties. As a workaround, I changed the return type from List to List and performed the serialization manually before returning the result. |
|
The empty derived objects are a serialization-boundary problem, not a failure of the polymorphic classes themselves. When a function is declared as returning List, a serializer that only has metadata for the base type can emit the base properties and drop derived members. JsonPolymorphic/JsonDerivedType attributes help only when the exact serializer options and type metadata used by AIFunctionFactory include them. For a tool contract, the most portable fix is an explicit tagged DTO rather than relying on .NET type metadata: public sealed record GeometryDto(
string Kind,
double? Radius = null,
double? Width = null,
double? Height = null,
double? Side = null);
// return IReadOnlyList<GeometryDto> from the toolThe model-facing schema now contains a stable discriminator (circle/rectangle/square), and the application can map the DTO back to GeometryCircle/GeometryRectangle/GeometrySquare after the tool call. If you keep the polymorphic return type, pass serializer options with a resolver/type-info configuration that registers every derived type to AIFunctionFactory.Create and verify the generated function schema; do not assume the attributes are picked up by a different default options instance. Manual serialization to JsonElement is a valid workaround when you need an open-ended payload, but it gives up useful schema validation. For tools, an explicit kind field plus nullable shape-specific fields is usually easier to validate, trace, and keep compatible across providers. |
Uh oh!
There was an error while loading. Please reload this page.
Does the Agent Framework support type descriptors during deserialization within the function tools mechanism (e.g., preserving polymorphic types or using metadata such as $type for correct object reconstruction)?
All reactions