Skip to content

.NET: Fix stop sequence array serialization#6701

Open
VectorPeak wants to merge 3 commits into
microsoft:mainfrom
VectorPeak:fix
Open

.NET: Fix stop sequence array serialization#6701
VectorPeak wants to merge 3 commits into
microsoft:mainfrom
VectorPeak:fix

Conversation

@VectorPeak

@VectorPeak VectorPeak commented Jun 24, 2026

Copy link
Copy Markdown

Motivation & Context

This PR fixes Chat Completions request serialization for multiple stop sequences by using the JSON metadata that matches the value being written.

Chat Completions requests support stop as either a single string or an array of strings. StopSequencesConverter already deserializes array values with string-list metadata, but the corresponding write path used message-content-part metadata when serializing StopSequences.Sequences.

The current write path was built with metadata for message content parts:

ChatCompletionsJsonContext.Default.IReadOnlyListMessageContentPart

That metadata describes message content values, not the IList<string> stored in StopSequences.Sequences. For requests that include multiple stop sequences, the converter should emit a JSON string array such as:

"stop": ["END", "STOP"]

Using the string-list metadata keeps the serializer consistent with the existing read path and with the expected Chat Completions request shape, while leaving single stop sequences and null stop values unchanged.

Description & Review Guide

What are the major changes?

  • Serialize StopSequences.Sequences with the existing string-list JSON type information:
ChatCompletionsJsonContext.Default.IListString
  • Add a focused serialization regression test for a Chat Completions request with multiple stop sequences.

What is the impact of these changes?

  • Multiple stop sequences are emitted as a JSON string array, matching the existing deserialization path and the expected Chat Completions request shape.
  • The affected path is CreateChatCompletion.Stop -> StopSequencesConverter.Write() -> StopSequences.Sequences -> the JSON stop field.
  • Single stop sequences and null stop values are unchanged.

What do you want reviewers to focus on?

  • Whether StopSequencesConverter should use the same string-list metadata for reading and writing stop sequence arrays.
  • Whether the regression test captures the multiple-stop-sequence request serialization path clearly enough.

Related Issue

No linked issue. This is a small deterministic bug fix found while reviewing the Chat Completions request serialization path. I searched open issues and PRs for matching StopSequences / stop sequence serialization reports and did not find an existing one.

Contribution Checklist

  • The code builds clean without any errors or warnings. Local build was not run because dotnet is not available in this environment.
  • All unit tests pass, and I have added new tests where possible. Added a focused unit test; local test execution was blocked because dotnet is not available in this environment.
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) - a workflow keeps the label and title prefix in sync automatically.

Copilot AI review requested due to automatic review settings June 24, 2026 07:33
@moonbox3 moonbox3 added the .NET Usage: [Issues, PRs], Target: .Net label Jun 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes .NET OpenAI ChatCompletions “stop” serialization so that multiple stop sequences are emitted as a JSON string array (matching the expected wire format) and adds a unit test to guard the behavior.

Changes:

  • Update StopSequencesConverter.Write to serialize Sequences using the source-generated IList<string> metadata instead of an unrelated type info.
  • Add a serialization unit test asserting "stop" is written as a JSON array when multiple stop sequences are provided.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
dotnet/tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/OpenAIChatCompletionsSerializationTests.cs Adds a regression test validating "stop" is serialized as a string array for multiple stop sequences.
dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/ChatCompletions/Models/StopSequences.cs Fixes the converter’s serialization path to use the correct source-generated type info for IList<string>.

@VectorPeak VectorPeak closed this Jun 24, 2026
@VectorPeak VectorPeak reopened this Jun 24, 2026
Thanks

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@VectorPeak

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants