-
Notifications
You must be signed in to change notification settings - Fork 2.2k
OpenAI.HttpClient to Azure.HttpClient mapper for test instrumentation #47416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jpalvarezl
merged 38 commits into
main
from
feature/stainless_httpclient_instrumentation
Dec 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
89496d3
Porting over changes from private repo
jpalvarezl eca457f
formatting
jpalvarezl 954ba7d
Update sdk/ai/azure-ai-agents/src/main/java/com/azure/ai/agents/imple…
jpalvarezl 4df3dba
Update sdk/ai/azure-ai-agents/src/main/java/com/azure/ai/agents/imple…
jpalvarezl 385e9b5
Formatting mismatch
jpalvarezl 0957fd2
Add async and error propagation test coverage for PolicyDecoratingHtt…
Copilot d113cc2
Add error handling tests for HttpClientHelper (#47428)
Copilot 224e3fb
Merged and improved assertions
jpalvarezl 672a6df
More PR feedback
jpalvarezl d4505e6
More PR feedback
jpalvarezl bf085da
More PR feedback
jpalvarezl 226c2c0
lazy mapping of types for the AzureHttpResponseAdapter
jpalvarezl 7c95643
Simplified mapping
jpalvarezl 867d0d6
Always using default httpPipeline
jpalvarezl 6bf38b7
Using bare minimum code
jpalvarezl 7c1b414
WIP: removing redundant code. Using default httpPipeline
jpalvarezl 96153b7
fix async tests
srnagar a4e5c29
Code style checks
jpalvarezl 8e760a9
Added recording for async test
jpalvarezl 94b0797
Merge branch 'main' into feature/stainless_httpclient_instrumentation
jpalvarezl b4bbdd3
Enabled sync tests and update assets
jpalvarezl b9a37e8
Updated test suite and restored value for context config
jpalvarezl ce40dab
Using latest version of azure-core-test
jpalvarezl a53bbd1
Forwarding request timeout and adding more custom machters for tests
jpalvarezl d9293fa
reassign addData result
jpalvarezl 27aa57f
WIP: timeout tests
jpalvarezl edcf29d
Timeout tests are run only in Live test mode
jpalvarezl 28d3d6e
Test asset update
jpalvarezl 275e2e9
Exceptions are allowed to propagate upwards
jpalvarezl 8c77862
Error mapping in place
jpalvarezl 2fcfb4d
reformat
jpalvarezl f4206bb
Format
jpalvarezl 67c9970
Fixed timeout tests
jpalvarezl 38694bb
PR feedback 1st round
jpalvarezl 6967620
PR feedback round 2: pom updates
jpalvarezl 41ca65d
dependency tag fixed and disabled test
jpalvarezl f0fc0ed
Disabled flacky timeout tests
jpalvarezl ad2d539
Disabled one more test
jpalvarezl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...gents/src/main/java/com/azure/ai/agents/implementation/http/AzureHttpResponseAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.agents.implementation.http; | ||
|
|
||
| import com.azure.core.http.HttpHeader; | ||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.openai.core.http.Headers; | ||
| import com.openai.core.http.HttpResponse; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.UncheckedIOException; | ||
|
|
||
| /** | ||
| * Adapter that exposes an Azure {@link com.azure.core.http.HttpResponse} as an OpenAI {@link HttpResponse}. This keeps | ||
| * the translation logic encapsulated so response handling elsewhere can remain framework agnostic. | ||
| */ | ||
| final class AzureHttpResponseAdapter implements HttpResponse { | ||
|
|
||
| private static final ClientLogger LOGGER = new ClientLogger(AzureHttpResponseAdapter.class); | ||
|
|
||
| private final com.azure.core.http.HttpResponse azureResponse; | ||
| private final Headers headers; | ||
| private final InputStream bodyStream; | ||
|
|
||
| /** | ||
| * Creates a new adapter instance for the provided Azure response. | ||
| * | ||
| * @param azureResponse Response returned by the Azure pipeline. | ||
| */ | ||
| AzureHttpResponseAdapter(com.azure.core.http.HttpResponse azureResponse) { | ||
| this.azureResponse = azureResponse; | ||
| this.headers = toOpenAiHeaders(azureResponse.getHeaders()); | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| this.bodyStream = azureResponse.getBodyAsBinaryData().toStream(); | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Override | ||
| public int statusCode() { | ||
| return azureResponse.getStatusCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public Headers headers() { | ||
| return headers; | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream body() { | ||
| return bodyStream; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| try { | ||
| bodyStream.close(); | ||
| } catch (IOException ex) { | ||
| throw LOGGER.logExceptionAsWarning(new UncheckedIOException("Failed to close response body stream", ex)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copies headers from the Azure response into the immutable OpenAI {@link Headers} collection. | ||
| */ | ||
| private static Headers toOpenAiHeaders(HttpHeaders httpHeaders) { | ||
| Headers.Builder builder = Headers.builder(); | ||
| for (HttpHeader header : httpHeaders) { | ||
| builder.put(header.getName(), header.getValuesList()); | ||
| } | ||
| return builder.build(); | ||
| } | ||
| } | ||
191 changes: 191 additions & 0 deletions
191
...ure-ai-agents/src/main/java/com/azure/ai/agents/implementation/http/HttpClientHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.agents.implementation.http; | ||
|
|
||
| import com.azure.core.http.HttpHeaderName; | ||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.util.BinaryData; | ||
| import com.azure.core.util.Context; | ||
| import com.azure.core.util.CoreUtils; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.openai.core.RequestOptions; | ||
| import com.openai.core.http.Headers; | ||
| import com.openai.core.http.HttpRequest; | ||
| import com.openai.core.http.HttpRequestBody; | ||
| import com.openai.core.http.HttpResponse; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * Utility entry point that adapts an Azure {@link com.azure.core.http.HttpClient} so it can be consumed by | ||
| * the OpenAI SDK generated clients. The helper performs request/response translation so that existing Azure | ||
| * pipelines, diagnostics, and retry policies can be reused without exposing the Azure HTTP primitives to | ||
| * callers that only understand the OpenAI surface area. | ||
| */ | ||
| public final class HttpClientHelper { | ||
|
|
||
| private static final ClientLogger LOGGER = new ClientLogger(HttpClientHelper.class); | ||
|
|
||
| private HttpClientHelper() { | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the given Azure {@link com.azure.core.http.HttpClient} with an implementation of the OpenAI | ||
| * {@link com.openai.core.http.HttpClient} interface. All requests and responses are converted on the fly. | ||
| * | ||
| * @param azureHttpClient The Azure HTTP client that should execute requests. | ||
| * @return A bridge client that honors the OpenAI interface but delegates execution to the Azure pipeline. | ||
| */ | ||
| public static com.openai.core.http.HttpClient httpClientMapper(com.azure.core.http.HttpClient azureHttpClient) { | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| return new HttpClientWrapper(azureHttpClient); | ||
| } | ||
|
|
||
| private static final class HttpClientWrapper implements com.openai.core.http.HttpClient { | ||
|
|
||
| private static final HttpHeaderName CONTENT_TYPE = HttpHeaderName.CONTENT_TYPE; | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final com.azure.core.http.HttpClient azureHttpClient; | ||
|
|
||
| private HttpClientWrapper(com.azure.core.http.HttpClient azureHttpClient) { | ||
| this.azureHttpClient = Objects.requireNonNull(azureHttpClient, "'azureHttpClient' cannot be null."); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // no-op | ||
| } | ||
|
|
||
| @Override | ||
| public HttpResponse execute(HttpRequest request) { | ||
| return execute(request, RequestOptions.none()); | ||
| } | ||
|
|
||
| @Override | ||
| public HttpResponse execute(HttpRequest request, RequestOptions requestOptions) { | ||
| Objects.requireNonNull(request, "request"); | ||
| Objects.requireNonNull(requestOptions, "requestOptions"); | ||
|
|
||
| com.azure.core.http.HttpRequest azureRequest = buildAzureRequest(request); | ||
| com.azure.core.http.HttpResponse azureResponse = this.azureHttpClient.sendSync(azureRequest, Context.NONE); | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| return new AzureHttpResponseAdapter(azureResponse); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<HttpResponse> executeAsync(HttpRequest request) { | ||
| return executeAsync(request, RequestOptions.none()); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<HttpResponse> executeAsync(HttpRequest request, RequestOptions requestOptions) { | ||
| Objects.requireNonNull(request, "request"); | ||
| Objects.requireNonNull(requestOptions, "requestOptions"); | ||
|
|
||
| final com.azure.core.http.HttpRequest azureRequest; | ||
| try { | ||
| azureRequest = buildAzureRequest(request); | ||
| } catch (RuntimeException runtimeException) { | ||
| return failedFuture(runtimeException); | ||
| } | ||
|
|
||
| return this.azureHttpClient.send(azureRequest) | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| .map(AzureHttpResponseAdapter::new) | ||
| .map(response -> (HttpResponse) response) | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| .toFuture(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts the OpenAI request metadata and body into an Azure {@link com.azure.core.http.HttpRequest}. | ||
| */ | ||
| private static com.azure.core.http.HttpRequest buildAzureRequest(HttpRequest request) { | ||
| HttpRequestBody requestBody = request.body(); | ||
| String contentType = requestBody != null ? requestBody.contentType() : null; | ||
| BinaryData bodyData = null; | ||
|
|
||
| if (requestBody != null) { | ||
| try { | ||
| bodyData = toBinaryData(requestBody); | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| } finally { | ||
| closeQuietly(requestBody); | ||
|
jpalvarezl marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| HttpHeaders headers = toAzureHeaders(request.headers()); | ||
| if (!CoreUtils.isNullOrEmpty(contentType) && headers.getValue(CONTENT_TYPE) == null) { | ||
| headers.set(CONTENT_TYPE, contentType); | ||
| } | ||
|
|
||
| com.azure.core.http.HttpRequest azureRequest | ||
| = new com.azure.core.http.HttpRequest(com.azure.core.http.HttpMethod.valueOf(request.method().name()), | ||
| OpenAiRequestUrlBuilder.buildUrl(request), headers); | ||
|
|
||
| if (bodyData != null) { | ||
| azureRequest.setBody(bodyData); | ||
| } | ||
|
|
||
| return azureRequest; | ||
| } | ||
|
|
||
| /** | ||
| * Copies OpenAI headers into an {@link HttpHeaders} instance so the Azure pipeline can process them. | ||
| */ | ||
| private static HttpHeaders toAzureHeaders(Headers sourceHeaders) { | ||
| HttpHeaders target = new HttpHeaders(); | ||
| sourceHeaders.names().forEach(name -> { | ||
| List<String> values = sourceHeaders.values(name); | ||
| HttpHeaderName headerName = HttpHeaderName.fromString(name); | ||
| if (values.isEmpty()) { | ||
| target.set(headerName, ""); | ||
| } else { | ||
| target.set(headerName, values); | ||
| } | ||
| }); | ||
| return target; | ||
| } | ||
|
jpalvarezl marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Buffers the OpenAI {@link HttpRequestBody} into {@link BinaryData} so it can be attached to the Azure | ||
| * request. The body is consumed exactly once and closed afterwards. | ||
| */ | ||
| private static BinaryData toBinaryData(HttpRequestBody requestBody) { | ||
| if (requestBody == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { | ||
| requestBody.writeTo(outputStream); | ||
| return BinaryData.fromBytes(outputStream.toByteArray()); | ||
| } catch (IOException e) { | ||
| throw LOGGER.logExceptionAsError(new UncheckedIOException("Failed to buffer request body", e)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Closes the OpenAI request body while suppressing any exceptions (to avoid masking the root cause). | ||
| */ | ||
| private static void closeQuietly(HttpRequestBody body) { | ||
| if (body == null) { | ||
| return; | ||
| } | ||
| try { | ||
| body.close(); | ||
| } catch (Exception ignored) { | ||
| // no-op | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a failed {@link CompletableFuture} for callers that require a future even when conversion fails. | ||
| */ | ||
| private static <T> CompletableFuture<T> failedFuture(Throwable throwable) { | ||
| CompletableFuture<T> future = new CompletableFuture<>(); | ||
| future.completeExceptionally(throwable); | ||
| return future; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.