Skip to content

Conversation

PederHP
Copy link
Collaborator

@PederHP PederHP commented Sep 1, 2025

Fixes #747

We might want to warn or give errors if StructuredContent is present when there are incompatible content blocks - ie more than one text content block. I don't think it can actually happen, but if it can it will be a spec violation. Clients should not get different output in unstructured Content if StructuredContent is provided.

@PederHP PederHP changed the title Fix structured output backwards compatibility for string literals Fix structured output backwards compatibility for string results Sep 1, 2025
// If there is structuredOutput we must stringify it, as the MCP specification requires Content to be for backwards compatibility
// but otherwise not differ
Content = [new TextContentBlock { Text = structuredContent == null ? text :
JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just resolve the type of structuredContent itself and skip one degree of indirection?

Suggested change
JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}],
JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo<JsonNode>()))}],

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just resolve the type of structuredContent itself and skip one degree of indirection?

Suggested change
JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo(typeof(object)))}],
JsonSerializer.Serialize(structuredContent, AIFunction.JsonSerializerOptions.GetTypeInfo<JsonNode>()))}],

Good point, but this goes for the default switch case too then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I suppose we can leave it for consistency.

@PederHP
Copy link
Collaborator Author

PederHP commented Sep 4, 2025

Should an error be thrown on the AIContent related return types? If we go by the intent expressed by dsp and others, and Content SHOULD be present for backwards compatibility, but (the part missing right now) MUST not differ from StructuredContent if it is - then there is no possible way to do with these.

It would be a breaking change, so probably best to wait with that until the spec is actually updated. Just wanted to mention that as pointed out in the issue, this is only a fix for the string case. I think that's the one most likely to cause trouble on actual production servers, so I still think it's worth fixing this one in isolation. Setting the attribute property to true for an AIContent or CallToolResult returning tool is a really exotic edge-case (to be fixed with the spec change imo - at the same time as any introduced schema validation requirements).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

StructuredContent vs Content Mismatch
2 participants