Skip to content

Apple Intelligence streaming schema responses are not raw json #32908

@mattleibow

Description

@mattleibow

Description

When using streaming responses in the Foundation Models, the response is actually a JSON object. We then have to serialize the response.

This means that responses are not sequential. The serialization process may reorder members. And the json is laways a complete JSON.

For example, if the model response is:

{"product":".NET MAUI","rating":5,"description":"This is the best things since sli

The Foundation Model response is a complete JSON object. This means that in order for us to return as a string, we have to serialize:

{"product":".NET MAUI","rating":5,"description":"This is the best things since sli"}

However, serialization is not deterministic and property order could change:

{"description":"This is the best things since sli","rating":5,"product":".NET MAUI"}

Workaround

For now, I am just adding a user message at the end that does this:

actualMessages.Add(new ChatMessage(ChatRole.User,
	$"""
	Please format your response as a JSON object according to the specified schema.
	Ensure that the JSON is well-formed and complete.
	
	IMPORTANT: DO NOT wrap with triple backticks! ONLY return the JSON object!

	{json.Schema}
	"""));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions