Skip to content

Commit 35e2728

Browse files
authored
Add stable web_search support and separate factory for web_search_preview (#815)
* Add support for stable "web_search"
1 parent ba45b1f commit 35e2728

25 files changed

+15794
-14665
lines changed

api/OpenAI.net8.0.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6058,7 +6058,8 @@ public class ResponseTool : IJsonModel<ResponseTool>, IPersistableModel<Response
60586058
public static ImageGenerationTool CreateImageGenerationTool(string model, ImageGenerationToolQuality? quality = null, ImageGenerationToolSize? size = null, ImageGenerationToolOutputFileFormat? outputFileFormat = null, int? outputCompressionFactor = null, ImageGenerationToolModerationLevel? moderationLevel = null, ImageGenerationToolBackground? background = null, ImageGenerationToolInputFidelity? inputFidelity = null, ImageGenerationToolInputImageMask inputImageMask = null, int? partialImageCount = null);
60596059
public static McpTool CreateMcpTool(string serverLabel, McpToolConnectorId connectorId, string authorizationToken = null, string serverDescription = null, IDictionary<string, string> headers = null, McpToolFilter allowedTools = null, McpToolCallApprovalPolicy toolCallApprovalPolicy = null);
60606060
public static McpTool CreateMcpTool(string serverLabel, Uri serverUri, string authorizationToken = null, string serverDescription = null, IDictionary<string, string> headers = null, McpToolFilter allowedTools = null, McpToolCallApprovalPolicy toolCallApprovalPolicy = null);
6061-
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null);
6061+
public static WebSearchPreviewTool CreateWebSearchPreviewTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null);
6062+
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null, WebSearchToolFilters filters = null);
60626063
protected virtual ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
60636064
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
60646065
protected virtual ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
@@ -6597,8 +6598,19 @@ public enum WebSearchCallStatus {
65976598
Failed = 3
65986599
}
65996600
[Experimental("OPENAI001")]
6601+
public class WebSearchPreviewTool : ResponseTool, IJsonModel<WebSearchPreviewTool>, IPersistableModel<WebSearchPreviewTool> {
6602+
public WebSearchPreviewTool();
6603+
public WebSearchToolContextSize? SearchContextSize { get; set; }
6604+
public WebSearchToolLocation UserLocation { get; set; }
6605+
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
6606+
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
6607+
protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
6608+
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
6609+
}
6610+
[Experimental("OPENAI001")]
66006611
public class WebSearchTool : ResponseTool, IJsonModel<WebSearchTool>, IPersistableModel<WebSearchTool> {
66016612
public WebSearchTool();
6613+
public WebSearchToolFilters Filters { get; set; }
66026614
public WebSearchToolContextSize? SearchContextSize { get; set; }
66036615
public WebSearchToolLocation UserLocation { get; set; }
66046616
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
@@ -6636,6 +6648,17 @@ public class WebSearchToolApproximateLocation : WebSearchToolLocation, IJsonMode
66366648
public override readonly string ToString();
66376649
}
66386650
[Experimental("OPENAI001")]
6651+
public class WebSearchToolFilters : IJsonModel<WebSearchToolFilters>, IPersistableModel<WebSearchToolFilters> {
6652+
public IList<string> AllowedDomains { get; set; }
6653+
[EditorBrowsable(EditorBrowsableState.Never)]
6654+
[Experimental("SCME0001")]
6655+
public ref JsonPatch Patch { get; }
6656+
protected virtual WebSearchToolFilters JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
6657+
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
6658+
protected virtual WebSearchToolFilters PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
6659+
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
6660+
}
6661+
[Experimental("OPENAI001")]
66396662
public class WebSearchToolLocation : IJsonModel<WebSearchToolLocation>, IPersistableModel<WebSearchToolLocation> {
66406663
[EditorBrowsable(EditorBrowsableState.Never)]
66416664
[Experimental("SCME0001")]

api/OpenAI.netstandard2.0.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5320,7 +5320,8 @@ public class ResponseTool : IJsonModel<ResponseTool>, IPersistableModel<Response
53205320
public static ImageGenerationTool CreateImageGenerationTool(string model, ImageGenerationToolQuality? quality = null, ImageGenerationToolSize? size = null, ImageGenerationToolOutputFileFormat? outputFileFormat = null, int? outputCompressionFactor = null, ImageGenerationToolModerationLevel? moderationLevel = null, ImageGenerationToolBackground? background = null, ImageGenerationToolInputFidelity? inputFidelity = null, ImageGenerationToolInputImageMask inputImageMask = null, int? partialImageCount = null);
53215321
public static McpTool CreateMcpTool(string serverLabel, McpToolConnectorId connectorId, string authorizationToken = null, string serverDescription = null, IDictionary<string, string> headers = null, McpToolFilter allowedTools = null, McpToolCallApprovalPolicy toolCallApprovalPolicy = null);
53225322
public static McpTool CreateMcpTool(string serverLabel, Uri serverUri, string authorizationToken = null, string serverDescription = null, IDictionary<string, string> headers = null, McpToolFilter allowedTools = null, McpToolCallApprovalPolicy toolCallApprovalPolicy = null);
5323-
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null);
5323+
public static WebSearchPreviewTool CreateWebSearchPreviewTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null);
5324+
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null, WebSearchToolFilters filters = null);
53245325
protected virtual ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
53255326
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
53265327
protected virtual ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
@@ -5802,8 +5803,18 @@ public enum WebSearchCallStatus {
58025803
Completed = 2,
58035804
Failed = 3
58045805
}
5806+
public class WebSearchPreviewTool : ResponseTool, IJsonModel<WebSearchPreviewTool>, IPersistableModel<WebSearchPreviewTool> {
5807+
public WebSearchPreviewTool();
5808+
public WebSearchToolContextSize? SearchContextSize { get; set; }
5809+
public WebSearchToolLocation UserLocation { get; set; }
5810+
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
5811+
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
5812+
protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
5813+
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
5814+
}
58055815
public class WebSearchTool : ResponseTool, IJsonModel<WebSearchTool>, IPersistableModel<WebSearchTool> {
58065816
public WebSearchTool();
5817+
public WebSearchToolFilters Filters { get; set; }
58075818
public WebSearchToolContextSize? SearchContextSize { get; set; }
58085819
public WebSearchToolLocation UserLocation { get; set; }
58095820
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
@@ -5838,6 +5849,15 @@ public class WebSearchToolApproximateLocation : WebSearchToolLocation, IJsonMode
58385849
public static bool operator !=(WebSearchToolContextSize left, WebSearchToolContextSize right);
58395850
public override readonly string ToString();
58405851
}
5852+
public class WebSearchToolFilters : IJsonModel<WebSearchToolFilters>, IPersistableModel<WebSearchToolFilters> {
5853+
public IList<string> AllowedDomains { get; set; }
5854+
[EditorBrowsable(EditorBrowsableState.Never)]
5855+
public ref JsonPatch Patch { get; }
5856+
protected virtual WebSearchToolFilters JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
5857+
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
5858+
protected virtual WebSearchToolFilters PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
5859+
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
5860+
}
58415861
public class WebSearchToolLocation : IJsonModel<WebSearchToolLocation>, IPersistableModel<WebSearchToolLocation> {
58425862
[EditorBrowsable(EditorBrowsableState.Never)]
58435863
public ref JsonPatch Patch { get; }

specification/base/typespec/responses/models.tsp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ union ToolType {
459459
file_search: "file_search",
460460
function: "function",
461461
computer_use_preview: "computer_use_preview",
462+
web_search: "web_search",
462463
web_search_preview: "web_search_preview",
463464
mcp: "mcp",
464465
code_interpreter: "code_interpreter",
@@ -613,7 +614,31 @@ model WebSearchPreviewTool extends Tool {
613614
@doc("""
614615
High level guidance for the amount of context window space to use for the search. One of `low`, `medium`, or `high`. `medium` is the default.
615616
""")
616-
search_context_size?: "low" | "medium" | "high";
617+
search_context_size?: SearchContextSize;
618+
}
619+
620+
// Tool customization (apply_discriminator): Apply discriminated type base for tools
621+
/** This tool searches the web for relevant results to use in a response. Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search). */
622+
model WebSearchTool extends Tool {
623+
/** The type of the web search tool. One of `web_search` or `web_search_2025_08_26`. */
624+
type: ToolType.web_search;
625+
626+
filters?: WebSearchToolFilters | null;
627+
628+
user_location?: Location | null;
629+
630+
/** High level guidance for the amount of context window space to use for the search. One of `low`, `medium`, or `high`. `medium` is the default. */
631+
search_context_size?: SearchContextSize;
632+
}
633+
634+
enum SearchContextSize {
635+
low,
636+
medium,
637+
high,
638+
}
639+
640+
model WebSearchToolFilters {
641+
allowed_domains?: string[] | null;
617642
}
618643

619644
// Tool customization: Establish a discriminated base for location approximation

specification/client/responses.client.tsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ using TypeSpec.HttpClient.CSharp;
214214
@@dynamicModel(ResponseCodeInterpreterCallInterpretingEvent);
215215
@@dynamicModel(TopLogProb);
216216
@@dynamicModel(LogProb);
217+
@@dynamicModel(WebSearchToolFilters);
217218
// custom.tsp models
218219
@@dynamicModel(ResponseErrorResponse);
219220
@@dynamicModel(DeleteResponseResponse);

src/Custom/Responses/Tools/ResponseTool.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ public static FileSearchTool CreateFileSearchTool(IEnumerable<string> vectorStor
5050
}
5151

5252
// CUSTOM: Added factory method as a convenience.
53-
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null)
53+
public static WebSearchTool CreateWebSearchTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null, WebSearchToolFilters filters = null)
5454
{
5555
return new WebSearchTool(
56+
kind: InternalToolType.WebSearch,
57+
patch: default,
58+
userLocation: userLocation,
59+
searchContextSize: searchContextSize,
60+
filters: filters);
61+
}
62+
63+
// CUSTOM: Added factory method as a convenience.
64+
public static WebSearchPreviewTool CreateWebSearchPreviewTool(WebSearchToolLocation userLocation = null, WebSearchToolContextSize? searchContextSize = null)
65+
{
66+
return new WebSearchPreviewTool(
5667
kind: InternalToolType.WebSearchPreview,
5768
patch: default,
5869
userLocation: userLocation,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OpenAI.Responses;
2+
3+
// CUSTOM: Renamed.
4+
[CodeGenType("WebSearchPreviewTool")]
5+
public partial class WebSearchPreviewTool
6+
{
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace OpenAI.Responses;
22

33
// CUSTOM: Renamed.
4-
[CodeGenType("WebSearchPreviewTool")]
4+
[CodeGenType("WebSearchTool")]
55
public partial class WebSearchTool
66
{
7-
}
7+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace OpenAI.Responses;
22

33
// CUSTOM: Renamed.
4-
[CodeGenType("WebSearchPreviewToolSearchContextSize")]
4+
[CodeGenType("SearchContextSize")]
55
public readonly partial struct WebSearchToolContextSize
66
{
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OpenAI.Responses;
2+
3+
// CUSTOM: Renamed.
4+
[CodeGenType("WebSearchToolFilters")]
5+
public partial class WebSearchToolFilters
6+
{
7+
}

src/Generated/Models/OpenAIContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,10 @@ namespace OpenAI
817817
[ModelReaderWriterBuildable(typeof(VectorStoreFileError))]
818818
[ModelReaderWriterBuildable(typeof(VectorStoreModificationOptions))]
819819
[ModelReaderWriterBuildable(typeof(WebSearchCallResponseItem))]
820+
[ModelReaderWriterBuildable(typeof(WebSearchPreviewTool))]
820821
[ModelReaderWriterBuildable(typeof(WebSearchTool))]
821822
[ModelReaderWriterBuildable(typeof(WebSearchToolApproximateLocation))]
823+
[ModelReaderWriterBuildable(typeof(WebSearchToolFilters))]
822824
[ModelReaderWriterBuildable(typeof(WebSearchToolLocation))]
823825
[ModelReaderWriterBuildable(typeof(WeightsAndBiasesIntegration))]
824826
public partial class OpenAIContext : ModelReaderWriterContext

0 commit comments

Comments
 (0)