Make RawRepresentation serializable, roundtripping as JsonElement#7392
Make RawRepresentation serializable, roundtripping as JsonElement#7392stephentoub wants to merge 1 commit intodotnet:mainfrom
Conversation
Change RawRepresentation from [JsonIgnore] to serializable across all AI abstraction types. On write, the converter serializes the runtime value using the active JsonSerializerOptions, falling back to an empty object if serialization fails. On read, the value is materialized as a JsonElement. Add RawRepresentationJsonConverter and update API baselines and tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@eiriktsarpalis, I'd appreciate your thoughts on this, whether it's a good idea in general, if so whether it's the right way to achieve it. We just keep getting sporadic complaints about RawRepresentation not persisting in any form through serialization. |
| /// </para> | ||
| /// </remarks> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public sealed class RawRepresentationJsonConverter : JsonConverter<object?> |
There was a problem hiding this comment.
With the exception of this eating serialization exceptions, this mostly coincides with how object serialization works out of the box. So maybe this could be simplified by delegating to the default object converter?
There was a problem hiding this comment.
Note that in the case of the built-in converter the actual representation can be controlled by the JsonUnknownTypeHandling enum.
| { | ||
| try | ||
| { | ||
| JsonSerializer.SerializeToElement(value, typeInfo).WriteTo(writer); |
There was a problem hiding this comment.
I'd probably move the WriteTo call outside the try block since we want to still surface any exceptions related to the underlying writer.
Consider using SerializeToDocument with a using so that pooled buffers are used for the intermediate representation.
I always assumed RawRepresentation contained values that were inherently nonserializable. The fact that we try to serialize it regardless doesn't feel great, but at least eating any exceptions means we're not blocking serialization if that fails. |
Summary
Make
RawRepresentationserializable across all AI abstraction types, with JSON roundtripping viaJsonElement.Changes
[JsonIgnore]with serialization support onRawRepresentationproperties across all AI abstraction types (AIContent,ChatMessage,ChatResponse,ChatResponseUpdate,ImageGenerationResponse,SpeechToTextResponse,SpeechToTextResponseUpdate,TextToSpeechResponse,TextToSpeechResponseUpdate,RealtimeConversationItem,RealtimeClientMessage,RealtimeServerMessage,AIAnnotation).RawRepresentationJsonConverter— a customJsonConverter<object?>that:JsonSerializerOptions. If serialization fails (e.g. circular references, missing type metadata), falls back to writing{}.JsonElement.JsonElementandJsonDocumentvalues to avoid redundant re-serialization.DistributedCachingChatClientto account forRawRepresentationnow participating in serialization.Microsoft.Extensions.AI.Abstractions.json).Microsoft Reviewers: Open in CodeFlow