dotnet / dotnet/extensions

Stripping function declarations on the last iteration when the history contains tool calls causes a ValidationError

Open
#7,713 0 comments 0 reactions 0 assignees View on GitHub
bug untriaged
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### Description

**Summary**

FunctionInvokingChatClient removes every AIFunctionDeclaration from ChatOptions.Tools on the final iteration (added in [#7207](https://github.com/dotnet/extensions/issues/7207)) so the inner client can't return a tool call nobody will execute.

That shape is is invalid on Amazon Bedrock's Converse API, which requires toolConfig whenever any message contains a toolUse/toolResult block. This means that on Bedrock, every conversation that uses a tool and then reaches MaximumIterationsPerRequest fails with:

```
Amazon.BedrockRuntime.Model.ValidationException:
The toolConfig field must be defined when using toolUse and toolResult content blocks.
```

### Reproduction Steps

**Repro**

Any FunctionInvokingChatClient over AWS.Bedrock.MEAI with at least one AIFunction registered, driven until the iteration cap is hit.

1. iteration >= MaximumIterationsPerRequest → PrepareOptionsForLastIteration (FunctionInvokingChatClient.cs).
2. Every registered tool is an AIFunction, hence an AIFunctionDeclaration, so the tool is not AIFunctionDeclaration filter keeps nothing: remainingTools stays null, options.Tools = null, and options.ToolMode = null. https://github.com/dotnet/extensions/blob/5682e2bb5ab1c92ff0d1e2c828320ba97cff5d76/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs#L1078
3. The Bedrock client builds toolConfig only when `options?.Tools is { Count: > 0 }` (https://github.com/aws/aws-dotnet-ai/blob/main/src/AWS.Bedrock.MEAI/BedrockChatClient.cs#L931)), so ToolConfig stays null and the field is omitted from the payload.
4. Bedrock rejects the request: history has tool blocks, payload has no toolConfig.

### Expected behavior

There should be no ValidationException from Bedrock.

### Actual behavior

```
Amazon.BedrockRuntime.Model.ValidationException:
The toolConfig field must be defined when using toolUse and toolResult content blocks.
```

### Regression?

_No response_

### Known Workarounds

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start in src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs at PrepareOptionsForLastIteration and the filtering logic around line 1078. Reproduce a conversation that reaches MaximumIterationsPerRequest with an AWS.Bedrock.MEAI client and inspect the outgoing request after toolUse/toolResult history exists. Done means the capped final request is accepted by Bedrock without the reported ValidationException.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, csharp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.