{"record":{"id":"ed160f03cc57758c","repo":"microsoft/semantic-kernel","slug":"unable-to-transform-result-into-typeof-toutput-n","errorCode":null,"errorMessage":"Unable to transform result into {typeof(TOutput).Name}","messagePattern":"Unable to transform result into (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Orchestration/Transforms/StructuredOutputTransform.cs","lineNumber":59,"sourceCode":"\n    /// <summary>\n    /// Transforms the provided <see cref=\"ChatMessageContent\"/> into a strongly-typed structured output by invoking the chat completion service and deserializing the response.\n    /// </summary>\n    /// <param name=\"messages\">The chat messages to process.</param>\n    /// <param name=\"cancellationToken\">A cancellation token to observe while waiting for the task to complete.</param>\n    /// <returns>The structured output of type <typeparamref name=\"TOutput\"/>.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the response cannot be deserialized into <typeparamref name=\"TOutput\"/>.</exception>\n    public async ValueTask<TOutput> TransformAsync(IList<ChatMessageContent> messages, CancellationToken cancellationToken = default)\n    {\n        ChatHistory history =\n            [\n                new ChatMessageContent(AuthorRole.System, this.Instructions),\n                .. messages,\n            ];\n        ChatMessageContent response = await this._service.GetChatMessageContentAsync(history, this._executionSettings, kernel: null, cancellationToken).ConfigureAwait(false);\n        return\n            JsonSerializer.Deserialize<TOutput>(response.Content ?? string.Empty) ??\n            throw new InvalidOperationException($\"Unable to transform result into {typeof(TOutput).Name}\");\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":62,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Orchestration/Transforms/StructuredOutputTransform.cs#L41-L62","documentation":"StructuredOutputTransform<TOutput>.TransformAsync calls a chat completion service, then does JsonSerializer.Deserialize<TOutput> on response.Content. If deserialization returns null (e.g. content is empty, \"null\", or non-JSON) it throws InvalidOperationException. Unlike the default transform, a JsonException from invalid JSON is NOT caught here — it propagates as a JsonException, while a successful parse to null hits this throw.","triggerScenarios":"The model returns an empty string (response.Content ?? string.Empty yields \"\"), returns the literal token `null`, or returns JSON whose root value is null; the model ignores the JSON instruction and returns prose that happens to parse to null.","commonSituations":"Execution settings lacking a JSON response-format constraint; using a model that does not reliably follow JSON instructions; empty/blank completion from a rate-limited or misconfigured endpoint; Instructions left at the default and the model returning null for nullable TOutput.","solutions":["Configure PromptExecutionSettings to enforce JSON output (e.g. response_format = json_object) if the connector supports it.","Strengthen the Instructions to forbid null/empty and require the target schema.","If the model cannot reliably produce JSON, switch to a stronger model or use the default transform with a JSON-emitting agent."],"exampleFix":"// before\nvar transform = new StructuredOutputTransform<Summary>(chat, new PromptExecutionSettings());\n\n// after\nvar settings = new PromptExecutionSettings { ExtensionData = new Dictionary<string, object> { [\"response_format\"] = new { type = \"json_object\" } } };\nvar transform = new StructuredOutputTransform<Summary>(chat, settings) { Instructions = \"Respond ONLY with JSON matching the Summary schema. Never return null.\" };","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"TOutput parsed;\ntry { parsed = await transform.TransformAsync(messages, ct); }\ncatch (InvalidOperationException ex)\n{\n    // model returned null/empty JSON; retry with stricter instructions or fail soft\n    throw new InvalidOperationException(\"Model did not return valid JSON for \" + typeof(TOutput).Name, ex);\n}","preventionTips":["Enforce JSON output in PromptExecutionSettings where the connector supports it.","Customize Instructions to forbid null/empty responses.","Validate that response.Content is non-empty JSON before deserializing in your own transform."],"tags":["orchestration","structured-output","serialization","chat-completion","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}