Skip to content

Commit 67584b9

Browse files
committed
Add OpenAIResponseClient.CreateResponse{Streaming}Async overloads that take RequestOptions
Today, if you're using the convenience types and you just want to set a user-agent header on a request (you're handed the instance and thus can't configure it at creation), the only supported option I see is to abandon the convenience methods and adopt the protocol methods, which means formatting all the input JSON and parsing all the response SSE / JSON. That's a big pill to swallow. Based on need, could we add a few convenience overloads that just take a RequestOptions instead of a CancellationToken? It results in minimal additional surface area / almost no additional duplicated code, as it's just taking a RequestOptions instead of taking a CancellationToken and calling ToRequestOptions on it. I've demonstrated in this PR with CreateResponseAsync and CreateResponseStreamingAsync, which are the two methods I currently care about.
1 parent 47b5d81 commit 67584b9

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/Custom/Responses/OpenAIResponseClient.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,23 @@ protected internal OpenAIResponseClient(ClientPipeline pipeline, string model, O
119119
[Experimental("OPENAI001")]
120120
public Uri Endpoint => _endpoint;
121121

122-
public virtual async Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
122+
public virtual Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
123+
{
124+
return CreateResponseAsync(inputItems, options, cancellationToken.ToRequestOptions());
125+
}
126+
127+
public virtual async Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options, RequestOptions requestOptions)
123128
{
124129
Argument.AssertNotNullOrEmpty(inputItems, nameof(inputItems));
130+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
131+
132+
if (requestOptions.BufferResponse is true)
133+
{
134+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'false' when calling 'CreateResponseAsync'.");
135+
}
125136

126137
using BinaryContent content = CreatePerCallOptions(options, inputItems, stream: false).ToBinaryContent();
127-
ClientResult protocolResult = await CreateResponseAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
138+
ClientResult protocolResult = await CreateResponseAsync(content, requestOptions).ConfigureAwait(false);
128139
OpenAIResponse convenienceValue = (OpenAIResponse)protocolResult;
129140
return ClientResult.FromValue(convenienceValue, protocolResult.GetRawResponse());
130141
}
@@ -161,14 +172,25 @@ public virtual ClientResult<OpenAIResponse> CreateResponse(string userInputText,
161172
}
162173

163174
public virtual AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
175+
{
176+
return CreateResponseStreamingAsync(inputItems, options, cancellationToken.ToRequestOptions(streaming: true));
177+
}
178+
179+
public virtual AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options, RequestOptions requestOptions)
164180
{
165181
Argument.AssertNotNullOrEmpty(inputItems, nameof(inputItems));
182+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
183+
184+
if (requestOptions.BufferResponse is false)
185+
{
186+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'true' when calling 'CreateResponseStreamingAsync'.");
187+
}
166188

167189
using BinaryContent content = CreatePerCallOptions(options, inputItems, stream: true).ToBinaryContent();
168190
return new AsyncSseUpdateCollection<StreamingResponseUpdate>(
169-
async () => await CreateResponseAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
191+
async () => await CreateResponseAsync(content, requestOptions).ConfigureAwait(false),
170192
StreamingResponseUpdate.DeserializeStreamingResponseUpdate,
171-
cancellationToken);
193+
requestOptions.CancellationToken);
172194
}
173195

174196
public virtual CollectionResult<StreamingResponseUpdate> CreateResponseStreaming(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)