Skip to content

Populate ChatMessage.MessageId in ToChatResponse and align with ResponseId#360

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-message-id-issue
Draft

Populate ChatMessage.MessageId in ToChatResponse and align with ResponseId#360
Copilot wants to merge 3 commits intomainfrom
copilot/fix-message-id-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 26, 2026

AbstractionMapper.ToChatResponse returned a ChatResponse with ResponseId set, but the contained ChatMessage had no MessageId. This caused incomplete identity mapping for completed chat responses.

  • Mapping fix

    • Generate the response identifier once in ToChatResponse.
    • Assign that identifier to both:
      • ChatResponse.ResponseId
      • ChatMessage.MessageId
    • Preserve existing behavior: prefer CreatedAtString, otherwise fall back to generated GUID.
  • Behavioral coverage

    • Extend ToChatCompletionMethod.Maps_Known_Properties to assert that response.Messages[0].MessageId is populated from CreatedAtString.
var responseId = stream.CreatedAtString ?? Guid.NewGuid().ToString("N");
var chatMessage = ToChatMessage(stream.Message);
chatMessage.CreatedAt = stream.CreatedAt;
chatMessage.MessageId = responseId;

return new ChatResponse(chatMessage)
{
    ResponseId = responseId,
    // ...
};
Original prompt

This section details on the original issue you should resolve

<issue_title>ChatResponseExtensions.ToChatResponse; No messageId present</issue_title>
<issue_description>

Discussed in #299

Originally posted by flintlouis August 14, 2025
Hi,
When using the OllamaApiClient, after getting the IAsyncEnumerable<ChatResponseUpdate> responseStream from GetStreamingResponseAsync(), I use the ChatResponseExtensions.ToChatResponse() from Micrsoft.Extensions.AI to squash all the seperate response chunks into a ChatResponse with a list of response messages. This extension uses the MessageId to distuingish which response chunks belong to which message and seperates it in this way.
But with the ChatResponseStream there are no message ID's implemented so this extension method just adds all AIContent into 1 message, even if it has a different role.

I was wondering, is it an idea to add messageId's to the ollama Message?

Example:
Before:
responseChunks =
[
{ role: assistant, content: [text] },
{ role: assistant, content: [tool_call] },
{ role: tool, content: [tool_response] },
{ role: assistant, content: [text] }
]
After:
response.Messages =
[
{ role: assistant, content: [text, tool_call, tool_response, text] }
]

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: awaescher <3630638+awaescher@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix missing messageId in ChatResponseExtensions Populate ChatMessage.MessageId in ToChatResponse and align with ResponseId Feb 26, 2026
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.

ChatResponseExtensions.ToChatResponse; No messageId present

2 participants