Skip to content

.Net: OpenAI: throw clear error when tool result call ID is missing#13945

Open
Utricularor wants to merge 2 commits intomicrosoft:mainfrom
Utricularor:issue-10125-missing-tool-call-id
Open

.Net: OpenAI: throw clear error when tool result call ID is missing#13945
Utricularor wants to merge 2 commits intomicrosoft:mainfrom
Utricularor:issue-10125-missing-tool-call-id

Conversation

@Utricularor
Copy link
Copy Markdown

@Utricularor Utricularor commented May 3, 2026

Motivation and Context

This pull request addresses issue #10125.

When a tool result is sent to the OpenAI connector without a tool call ID, the failure mode is not clear to contributors and users. In practice, this can happen when function calls are constructed synthetically (for example, without a provider-generated call ID). Instead of failing later in a less obvious way, this change makes the behavior explicit and easier to diagnose.

Problem Summary

The OpenAI request conversion path accepts tool result messages (AuthorRole.Tool) and transforms FunctionResultContent items into OpenAI tool messages.

Before this change:

  • A missing FunctionResultContent.CallId could flow into message conversion.
  • The resulting error was indirect and not actionable.

Expected behavior:

  • Fail fast with a clear error message that explains what is missing and how to fix it.

What Changed

1) Added explicit validation in OpenAI tool-result conversion

File changed:

  • dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs

Change:

  • Added a guard for string.IsNullOrWhiteSpace(resultContent.CallId) in the tool-result handling path.
  • If the call ID is missing, the code now throws ArgumentException with a clear, actionable message.

Why:

  • ool_call_id is required when constructing OpenAI tool messages.
  • This makes the contract explicit and prevents hidden downstream failures.

2) Added a regression unit test

File changed:

  • dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Services/OpenAIChatCompletionServiceTests.cs

New test:

  • FunctionResultsWithoutCallIdThrowClearExceptionAsync

What it validates:

  • Given a tool message containing FunctionResultContent with callId: null,
  • The OpenAI chat completion flow throws ArgumentException,
  • And the message clearly indicates the missing tool call ID.

Why:

  • Prevents regressions and documents expected behavior for future contributors.

Validation

I ran targeted connector tests for this area:

  • dotnet test Connectors.OpenAI.UnitTests.csproj --filter FullyQualifiedName~OpenAIChatCompletionServiceTests.FunctionResults

Result:

  • 3 passed, 0 failed.

Impact and Compatibility

  • Scope is limited to OpenAI connector tool-result message conversion.
  • No public API shape changes.
  • Behavior change is intentional: invalid input now fails early with a clearer exception.

Notes for Reviewers

As a newer contributor, I focused on making the fix minimal and explicit:

  • one small runtime guard where the invalid state appears,
  • one regression test to lock the behavior.

If maintainers prefer a different exception type or wording, I can update quickly.

Copilot AI review requested due to automatic review settings May 3, 2026 00:08
@Utricularor Utricularor requested a review from a team as a code owner May 3, 2026 00:08
@moonbox3 moonbox3 added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels May 3, 2026
@github-actions github-actions Bot changed the title OpenAI: throw clear error when tool result call ID is missing .Net: OpenAI: throw clear error when tool result call ID is missing May 3, 2026
Copy link
Copy Markdown
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

This PR addresses dotnet issue #10125 by failing fast when OpenAI tool result messages are missing a required tool call ID, preventing later null/invalid-id failures and providing a clearer error for callers.

Changes:

  • Added explicit validation that FunctionResultContent.CallId is present when converting tool result messages into OpenAI request messages.
  • Throws an ArgumentException with a clearer message when the call ID is missing/blank.
  • Added a regression unit test covering the missing call ID scenario.

Reviewed changes

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

File Description
dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs Adds call-id validation for tool result message conversion to OpenAI ToolChatMessage.
dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Services/OpenAIChatCompletionServiceTests.cs Adds regression test ensuring a clear ArgumentException is thrown when callId is missing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if (string.IsNullOrWhiteSpace(resultContent.CallId))
{
throw new ArgumentException("Function result message is missing a tool call id. Provide FunctionResultContent.CallId when sending tool results to OpenAI.");
}

[Fact]
public async Task FunctionResultsWithoutCallIdThrowClearExceptionAsync()
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 91% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by Utricularor's agents

@Utricularor
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

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants