Skip to content

Commit 8378659

Browse files
committed
Fix Azure native tool usage fixtures and release 1.0.2
1 parent 41fc3cf commit 8378659

20 files changed

Lines changed: 232 additions & 16 deletions

‎Directory.Build.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<AnalysisLevel>latest-recommended</AnalysisLevel>
1212
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1313
<NoWarn>$(NoWarn);CS1591;CA1707;CA1848;CA1859;CA1861;CA1873;MEAI001</NoWarn>
14-
<Version>1.0.1</Version>
14+
<Version>1.0.2</Version>
1515
<PackageVersion>$(Version)</PackageVersion>
1616
</PropertyGroup>
1717

‎docs/Features/NativeBuiltInToolContractInventory.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ flowchart LR
2828
Stream --> SDK
2929
```
3030

31+
## Azure OpenAI Responses
32+
33+
`/azure-openai/openai/v1/responses` accepts the bounded completed web/file/code fixtures described below. Configure Azure billable web requests independently using `WithWebSearchRequests(int?)`; JSON and terminal SSE expose `tool_usage.web_search.num_requests`. Null omits unavailable usage; zero remains an explicit provider quantity. See [Azure metering fixtures](OpenAiNativeWebSearch.md#azure-billable-request-fixtures). No container session or duration charge is inferred from code output items.
34+
3135
## OpenAI Responses
3236

3337
Endpoint: upstream `POST /v1/responses`; hosted namespace `/openai/v1/responses`. Standard Bearer authentication.

‎docs/Features/OpenAiNativeFileCode.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ These fixtures serve `POST /openai/v1/responses`. They return configured native
1212
| `file_search` | `gpt-4o-mini` | [Official file-search cookbook](https://developers.openai.com/cookbook/examples/file_search_responses) |
1313
| `code_interpreter` | `gpt-4.1` | [Official tool cookbook](https://developers.openai.com/cookbook/examples/mcp/mcp_tool_guide) |
1414

15-
The pairings are separate. A current request cannot declare multiple native families whose supported model sets have no common member. Raw model checks apply to current declarations and named choices; completed prior tool outputs and include selectors alone do not re-qualify a past tool against the current model. The core independently checks the qualified native surface and selected fixture. Other provider namespaces and Chat Completions reject native declarations/history.
15+
The pairings are separate. A current request cannot declare multiple native families whose supported model sets have no common member. Raw model checks apply to current declarations and named choices; completed prior tool outputs and include selectors alone do not re-qualify a past tool against the current model. The core independently checks the qualified native surface and selected fixture. Azure OpenAI Responses also accepts these bounded native declarations at `/azure-openai/openai/v1/responses`; other provider namespaces and Chat Completions reject them. Azure code results retain the exact fixture container identity and do not invent session counts or durations.
1616

1717
## Configure a file-search fixture
1818

‎docs/Features/OpenAiNativeWebSearch.md‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ retention remains bounded and explicitly marks omitted payload evidence.
116116

117117
This slice makes no support claim for `web_search_preview`, open/find actions, native tool failures,
118118
non-ASCII citation-offset semantics, external-web-access/return-token-budget controls,
119-
resource lifecycle or native tools on other provider namespaces. File search and code interpreter use their
119+
resource lifecycle or native tools on provider namespaces other than Azure OpenAI Responses. File search and code interpreter use their
120120
separate [bounded fixture contract](OpenAiNativeFileCode.md). Remaining contracts are tracked in the
121121
[native contract inventory](NativeBuiltInToolContractInventory.md).
122122

@@ -153,3 +153,33 @@ release or main merge is asserted by these local results.
153153
The independent code-only checkout passed 965/965 server integrations after the ordinary-response regression fix, with 91.96% production line coverage, 77.38% branch coverage and 94.38% method coverage. Canonical formatting and Release build passed with 0 warnings/errors. These results exclude the parallel owner analytics/visual work. Evidence: `artifacts/delivery/native-isolated-final-{format,build,coverage}.log`.
154154

155155
The same code-only checkout packed all 19 packages; the fresh installed NuGet/Aspire/official SDK/Chromium consumer passed 2/2. Its .NET SDK-built linux/arm64 image passed actual HTTP native JSON/SSE and terminal-evidence acceptance. Evidence: `artifacts/delivery/native-isolated-{pack,consumer,container-publish,container-smoke}.log`; container evidence `artifacts/native-code-validation/artifacts/container-verification/220fcd85c2a76ed5`. No public package/image publication is claimed.
156+
157+
## Azure billable request fixtures
158+
159+
Azure OpenAI Responses accepts the same native web/file/code declarations at
160+
`/azure-openai/openai/v1/responses`. Configure Azure's exact billable search quantity separately
161+
from output actions; one `web_search_call` may represent three Bing requests:
162+
163+
```csharp
164+
await control.ConfigureAsync(config => config.UseGpt41Mini()
165+
.UseChatScenario("azure-search", scenario => scenario
166+
.RespondsWithItems(
167+
new LlmTestServerResponseWebSearchItem { Id = "ws_meter", Queries = ["fixture facts"] },
168+
new LlmTestServerResponseMessageItem { Content = "Scripted answer." })
169+
.WithWebSearchRequests(3)));
170+
```
171+
172+
`WithWebSearchRequests(int?)` also works on conversation steps. The public scenario response
173+
field is `WebSearchRequests` (`webSearchRequests` in configuration JSON). Null or omission means
174+
usage unavailable; zero is an explicit provider zero; negative quantities are rejected before
175+
configuration changes. Azure JSON and terminal SSE responses emit `tool_usage.web_search.num_requests`.
176+
Early stream responses omit this final meter. OpenAI responses retain their native output items and
177+
omit Azure-specific usage. The official Responses SDK preserves Azure's extension in its JSON model.
178+
179+
This quantity is never calculated from output items, queries or search actions. This follows
180+
[Azure's billable usage contract](https://learn.microsoft.com/azure/foundry/openai/how-to/web-search#web-search-usage-and-billing).
181+
OpenAI search charges use their own output-call semantics, while Anthropic's existing basic web-search
182+
fixtures expose `usage.server_tool_use.web_search_requests`; neither is converted into an Azure meter.
183+
Code interpreter fixtures preserve their configured container ID and native outputs. They do not invent
184+
per-response container sessions or billing duration: container/session accounting requires a separate
185+
lifecycle contract from the provider rather than a count of code calls.

‎src/ManagedCode.LlmTestServer.Hosting/LlmTestServerEndpointRouteBuilderExtensions.Streaming.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ private static async Task WriteStreamingResponseAsync(
8080
long created,
8181
OpenAiCacheUsageShape cacheUsageShape,
8282
CancellationToken cancellationToken,
83-
IReadOnlyCollection<string>? includedProperties = null
83+
IReadOnlyCollection<string>? includedProperties = null,
84+
bool azureToolUsage = false
8485
)
8586
{
8687
context.Response.StatusCode = StatusCodes.Status200OK;
8788
context.Response.ContentType = "text/event-stream";
8889

8990
LlmTestServerStreamDeliveryEvidence.Begin(context, result.StreamDelivery);
90-
foreach (var streamEvent in OpenAiWireMapper.ToResponseStreamEvents(result, responseId, created, cacheUsageShape, includedProperties))
91+
foreach (var streamEvent in OpenAiWireMapper.ToResponseStreamEvents(result, responseId, created, cacheUsageShape, includedProperties, azureToolUsage))
9192
{
9293
await WriteResponseSseDataAsync(context, streamEvent, cancellationToken).ConfigureAwait(false);
9394
if (result.StreamDelivery is not null)

‎src/ManagedCode.LlmTestServer.Hosting/LlmTestServerEndpointRouteBuilderExtensions.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,10 @@ private static async Task<IResult> CreateResponseCoreAsync(
11561156
CancellationToken cancellationToken
11571157
)
11581158
{
1159-
var allowNativeTools = context.Request.Path.Equals("/openai/v1/responses", StringComparison.Ordinal);
1159+
var azureToolUsage = context.Request.Path.StartsWithSegments(ProviderRoutes.AzureOpenAI) &&
1160+
context.GetEndpoint()?.Metadata.GetMetadata<LlmTestServerProviderOperationMetadata>()
1161+
?.OperationId == LlmTestServerProviderOperationIds.AzureOpenAI.V1ResponsesCreate;
1162+
var allowNativeTools = azureToolUsage || context.Request.Path.Equals("/openai/v1/responses", StringComparison.Ordinal);
11601163
var read = await ReadJsonAsync<OpenAiResponseRequest>(context, cancellationToken,
11611164
validation: allowNativeTools ? OpenAiRequestValidation.ValidateNativeResponse : OpenAiRequestValidation.ValidateResponse)
11621165
.ConfigureAwait(false);
@@ -1226,13 +1229,14 @@ await WriteStreamingResponseAsync(
12261229
created,
12271230
cacheUsageShape,
12281231
cancellationToken,
1229-
request.Include
1232+
request.Include,
1233+
azureToolUsage
12301234
)
12311235
.ConfigureAwait(false);
12321236
return Results.Empty;
12331237
}
12341238

1235-
return Results.Json(OpenAiWireMapper.ToResponse(result, responseId, created, cacheUsageShape, request.Include));
1239+
return Results.Json(OpenAiWireMapper.ToResponse(result, responseId, created, cacheUsageShape, request.Include, azureToolUsage));
12361240
}
12371241

12381242
private static async Task<IResult> CompleteAnthropicMessageAsync(

‎src/ManagedCode.LlmTestServer.OpenAI/OpenAiResponseModels.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public sealed record OpenAiResponse
6060
[JsonPropertyName("usage")]
6161
public OpenAiResponseUsage Usage { get; init; } = new();
6262

63+
[JsonPropertyName("tool_usage")]
64+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
65+
public OpenAiResponseToolUsage? ToolUsage { get; init; }
66+
6367
[JsonPropertyName("incomplete_details")]
6468
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
6569
public OpenAiResponseIncompleteDetails? IncompleteDetails { get; init; }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace ManagedCode.LlmTestServer.OpenAI;
4+
5+
/// <summary>Azure Responses billable tool usage, distinct from output tool calls.</summary>
6+
public sealed record OpenAiResponseToolUsage
7+
{
8+
[JsonPropertyName("web_search")]
9+
public OpenAiResponseWebSearchUsage WebSearch { get; init; } = new();
10+
}
11+
12+
public sealed record OpenAiResponseWebSearchUsage
13+
{
14+
[JsonPropertyName("num_requests")]
15+
public int NumberOfRequests { get; init; }
16+
}

‎src/ManagedCode.LlmTestServer.OpenAI/OpenAiWireMapper.NativeResponses.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ private static OpenAiResponseOutputItem MapResponseItem(LlmTestServerResponseIte
100100

101101
public static IEnumerable<object> ToResponseStreamEvents(LlmTestServerChatResult result, string responseId,
102102
long created, OpenAiCacheUsageShape cacheUsageShape = OpenAiCacheUsageShape.None,
103-
IReadOnlyCollection<string>? includedProperties = null)
103+
IReadOnlyCollection<string>? includedProperties = null, bool azureToolUsage = false)
104104
{
105-
var response = ToResponse(result, responseId, created, cacheUsageShape, includedProperties);
105+
var response = ToResponse(result, responseId, created, cacheUsageShape, includedProperties, azureToolUsage);
106106
var sequence = 0;
107-
yield return new { type = "response.created", sequence_number = sequence++, response = response with { Status = "in_progress", IncompleteDetails = null, Output = [] } };
108-
yield return new { type = "response.in_progress", sequence_number = sequence++, response = response with { Status = "in_progress", IncompleteDetails = null, Output = [] } };
107+
yield return new { type = "response.created", sequence_number = sequence++, response = response with { Status = "in_progress", IncompleteDetails = null, Output = [], ToolUsage = null } };
108+
yield return new { type = "response.in_progress", sequence_number = sequence++, response = response with { Status = "in_progress", IncompleteDetails = null, Output = [], ToolUsage = null } };
109109
for (var index = 0; index < response.Output.Count; index++)
110110
{
111111
var item = response.Output[index];

‎src/ManagedCode.LlmTestServer.OpenAI/OpenAiWireMapper.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public static OpenAiResponse ToResponse(
147147
string responseId,
148148
long created,
149149
OpenAiCacheUsageShape cacheUsageShape = OpenAiCacheUsageShape.None,
150-
IReadOnlyCollection<string>? includedProperties = null
150+
IReadOnlyCollection<string>? includedProperties = null,
151+
bool azureToolUsage = false
151152
)
152153
{
153154
return new()
@@ -156,6 +157,8 @@ public static OpenAiResponse ToResponse(
156157
CreatedAt = created,
157158
Model = result.ModelId,
158159
Output = CreateResponseOutput(result, includedProperties),
160+
ToolUsage = azureToolUsage && result.WebSearchRequests is { } requests
161+
? new() { WebSearch = new() { NumberOfRequests = requests } } : null,
159162
Status = result.OutputDisposition?.Kind == LlmTestServerOutputDispositionKind.Filtered ? "incomplete" : "completed",
160163
IncompleteDetails = result.OutputDisposition?.Kind == LlmTestServerOutputDispositionKind.Filtered ? new() : null,
161164
Usage = new OpenAiResponseUsage

0 commit comments

Comments
 (0)