From 7b1f07019a19cf0f23263f65a4069cf5d35a2c18 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 25 Oct 2025 19:58:52 +0000 Subject: [PATCH 1/3] feat: Implement OpenAI Responses API This commit introduces the implementation of the OpenAI Responses API, allowing users to leverage tools like web search. - Adds `response.go` with the `CreateResponseRequest` and `CreateResponseResponse` structs and the `CreateResponse` client method. - Includes `response_test.go` with a unit test to verify the new functionality. --- response.go | 45 +++++++++++++++++++++++++++++++++++++++ response_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 response.go create mode 100644 response_test.go diff --git a/response.go b/response.go new file mode 100644 index 000000000..2f3fd19bd --- /dev/null +++ b/response.go @@ -0,0 +1,45 @@ +package openai + +import ( + "context" + "net/http" +) + +const ( + responsesSuffix = "/responses" +) + +type CreateResponseRequest struct { + Model string `json:"model"` + Input any `json:"input"` + Tools []Tool `json:"tools,omitempty"` + PreviousResponseID string `json:"previous_response_id,omitempty"` +} + +type CreateResponseResponse struct { + ID string `json:"id"` + Created int64 `json:"created_at"` + Error any `json:"error,omitempty"` + Output []any `json:"output"` + Model string `json:"model"` + + httpHeader +} + +func (c *Client) CreateResponse( + ctx context.Context, + request CreateResponseRequest, +) (response CreateResponseResponse, err error) { + req, err := c.newRequest( + ctx, + http.MethodPost, + c.fullURL(responsesSuffix), + withBody(request), + ) + if err != nil { + return + } + + err = c.sendRequest(req, &response) + return +} diff --git a/response_test.go b/response_test.go new file mode 100644 index 000000000..9ee16d93e --- /dev/null +++ b/response_test.go @@ -0,0 +1,55 @@ +package openai_test + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strconv" + "testing" + "time" + + "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test/checks" +) + +func TestCreateResponse(t *testing.T) { + client, server, teardown := setupOpenAITestServer() + defer teardown() + server.RegisterHandler("/v1/responses", handleResponseEndpoint) + _, err := client.CreateResponse(context.Background(), openai.CreateResponseRequest{ + Model: "gpt-4o", + Input: "What's the latest news about AI?", + Tools: []openai.Tool{ + { + Type: "web_search", + }, + }, + }) + checks.NoError(t, err, "CreateResponse error") +} + +func handleResponseEndpoint(w http.ResponseWriter, r *http.Request) { + var err error + var resBytes []byte + + if r.Method != "POST" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + } + + var responseReq openai.CreateResponseRequest + if err = json.NewDecoder(r.Body).Decode(&responseReq); err != nil { + http.Error(w, "could not read request", http.StatusInternalServerError) + return + } + + res := openai.CreateResponseResponse{ + ID: "resp_" + strconv.Itoa(int(time.Now().Unix())), + Created: time.Now().Unix(), + Model: responseReq.Model, + Output: []any{}, + } + + resBytes, _ = json.Marshal(res) + fmt.Fprintln(w, string(resBytes)) +} From f808d53c4302b32563393d6765ca9637b5cd6e97 Mon Sep 17 00:00:00 2001 From: Igor Besel Date: Sat, 25 Oct 2025 22:21:53 +0200 Subject: [PATCH 2/3] Update response_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- response_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/response_test.go b/response_test.go index 9ee16d93e..09c1e975f 100644 --- a/response_test.go +++ b/response_test.go @@ -35,6 +35,7 @@ func handleResponseEndpoint(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return } var responseReq openai.CreateResponseRequest From 8ba2d4ac34df8fc5b0a97ae724ba69bfa145d2cb Mon Sep 17 00:00:00 2001 From: Igor Besel Date: Sun, 26 Oct 2025 12:58:25 +0100 Subject: [PATCH 3/3] Update response.go --- response.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/response.go b/response.go index 2f3fd19bd..21bb141f9 100644 --- a/response.go +++ b/response.go @@ -10,10 +10,10 @@ const ( ) type CreateResponseRequest struct { - Model string `json:"model"` - Input any `json:"input"` - Tools []Tool `json:"tools,omitempty"` - PreviousResponseID string `json:"previous_response_id,omitempty"` + Model string `json:"model"` + Input any `json:"input"` + Tools []Tool `json:"tools,omitempty"` + PreviousResponseID string `json:"previous_response_id,omitempty"` } type CreateResponseResponse struct {