Skip to content

Comments

.NET: Fix JSON arrays of objects parsed as empty records when no schema is defined#4199

Open
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/4195-json-array-empty-records
Open

.NET: Fix JSON arrays of objects parsed as empty records when no schema is defined#4199
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/4195-json-array-empty-records

Conversation

@LEDazzio01
Copy link
Contributor

Motivation and Context

Fixes #4195 — JSON arrays of objects are parsed as empty records when no schema is defined in declarative workflows.

When an InvokeAzureAgent action returns a JSON object containing an array of objects, and the output is captured via responseObject, all nested object properties are silently dropped. Each object in the array becomes {}. Subsequent Foreach iteration over the array crashes with an unhandled workflow failure.

Description

Root cause: In JsonDocumentExtensions.cs, the ParseTable method's DetermineElementType() function creates a schema-bound VariableType with an empty (non-null) schema when targetType.Schema is null:

// Before (broken)
JsonValueKind.Object => VariableType.Record(targetType.Schema?.Select(kvp => (kvp.Key, kvp.Value)) ?? []),

When targetType.Schema is null, the ?? [] fallback passes an empty collection to VariableType.Record(), creating a VariableType with Schema set to an empty FrozenDictionary (non-null, count = 0). Downstream, ParseRecord checks targetType.Schema is null and takes the ParseSchema path (which iterates over zero fields), silently discarding all JSON properties.

Fix: Check targetType.HasSchema (which correctly handles empty schemas) and fall back to VariableType.RecordType when no schema is defined:

// After (fixed)
JsonValueKind.Object => targetType.HasSchema
    ? VariableType.Record(targetType.Schema!.Select(kvp => (kvp.Key, kvp.Value)))
    : VariableType.RecordType,

This ensures ParseRecord takes the dynamic ParseValues() path that preserves all JSON properties.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? No — this is a bug fix that restores expected behavior.

…ords

When parsing JSON arrays containing objects without a predefined schema,
`DetermineElementType()` was creating a `VariableType` with an empty
(non-null) schema via `targetType.Schema?.Select(...) ?? []`. This caused
`ParseRecord` to take the schema-based parsing path, iterating over zero
schema fields and silently discarding all JSON properties.

The fix checks `targetType.HasSchema` and falls back to
`VariableType.RecordType` (which has `Schema = null`) when no schema is
defined, ensuring `ParseRecord` takes the dynamic `ParseValues()` path
that preserves all JSON properties.

Closes microsoft#4195
Copilot AI review requested due to automatic review settings February 23, 2026 23:01
@LEDazzio01
Copy link
Contributor Author

I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.

@markwallace-microsoft markwallace-microsoft added .NET workflows Related to Workflows in agent-framework labels Feb 23, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a declarative workflow JSON parsing bug where arrays of objects could be inferred as “empty-schema records,” causing nested object properties to be dropped when no schema is defined.

Changes:

  • Updates ParseTable element-type inference to use VariableType.RecordType when the target list type has no schema, avoiding creation of an empty (non-null) schema.
  • Uses targetType.HasSchema to decide when to propagate a schema into inferred record element types.

Comment on lines +114 to +116
JsonValueKind.Object => targetType.HasSchema
? VariableType.Record(targetType.Schema!.Select(kvp => (kvp.Key, kvp.Value)))
: VariableType.RecordType,
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

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

Add a regression unit test for the schema-less path this fixes: parsing a JSON object (using VariableType.RecordType) that contains an array of objects should preserve nested object properties (e.g., { "items": [ {"name":"Alice"} ] } should yield dictionaries with a name key). Current unit tests cover record parsing without schema, and arrays of records with an explicit schema, but not arrays of objects when the array element type must be inferred with no schema (the scenario that previously produced {} elements).

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@crickman crickman Feb 24, 2026

Choose a reason for hiding this comment

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

@LEDazzio01 - I agree that a unit-test would be a great way to demonstrate the fix. A unit-test fixture already exists here: https://github.com/microsoft/agent-framework/blob/main/dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/Extensions/JsonDocumentExtensionsTests.cs

AI might be able to quickly add a unit-test. I'm confident we can get this merged as soon as the test is ready.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add the unit test when I get home from work late this afternoon :)

@crickman crickman requested review from crickman February 24, 2026 17:28
@crickman crickman self-assigned this Feb 24, 2026
@crickman crickman moved this to In Review in Agent Framework Feb 24, 2026
@crickman crickman added the bug Something isn't working label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working declarative-workflow .NET workflows Related to Workflows in agent-framework

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: Declarative workflow - JSON arrays of objects parsed as empty records when no schema is defined

3 participants