Skip to content

Sampling with Tools fails with 400 error from OpenAI #1127

@mikekistler

Description

@mikekistler

Describe the bug

I'm trying to use the new "Sampling with Tools" feature of the 2025-11-25 version of the spec, implemented in the C# SDK in #976.

My application fails at the point where the sampling handler returns the tool call result to the LLM. The error (which was difficult to catch) is:

Image

I think this is a bug in the SDK. I fed the error message to GitHub Copilot and it says (extracting just the key bit):

The problem was that the default samplingHandler from CreateSamplingHandler() wasn't properly converting between MCP protocol message formats and OpenAI API message formats. Specifically:

  • MCP protocol uses Role.Assistant with ToolUseContentBlock and Role.User with ToolResultContentBlock
  • OpenAI API expects Role.Assistant1 with tool_calls and Role.Tool]` with tool results

The code in CreateSamplingHandler() to set the Role is:

messages.Add(new ChatMessage(sm.Role is Role.Assistant ? ChatRole.Assistant : ChatRole.User, aiContents));

So it sets tool results to ChatRole.User so OpenAI doesn't see them as tool results.

To Reproduce

Code for the repro is in the issue-repro branch of my SamplingWithToolsDemo repo.

You will need a GitHub PAT with models permissions, set in the GITHUB_TOKEN environment variable.

Start the server:

cd server
dotnet run

Start the client in the debugger. Set a break point on the call to samplingHandler(c, p, t) -- line 51 of client/Program.cs.

At the Prompt, enter

Play a single game of craps. Report the results of each roll and explain how it affects the outcome of the game.

The break point will hit twice. When it hits the second time, enable break on "All Exceptions". Then continue.

Expected behavior

The call to OpenAI with tool call results should succeed.

Logs

Will be happy to provide any if needed.

Additional context

I made a copy of CreateSamplingHandler() and changed the code to set the ChatRole to Tool for any messages with tool call results and the app then worked successfully. I'll push a copy of this code up to my repo shortly.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions