Skip to content

Go

The official `openai-go` SDK pointed at the gateway, with streaming and reading the refusal body.

Updated 02 Nov 2026Checked against ai-firewall@339de55, cli@4c8c3b7
vulnetix ai-firewall snippet --lang go --sdk openai --provider openai --output-file main.go

prints this program with your UUID and an allowed model filled in. By hand:

go mod init hello && go get github.com/openai/openai-go
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"os"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	client := openai.NewClient(
		option.WithBaseURL("https://guardrails.vulnetix.com/openai/"+os.Getenv("VULNETIX_ORG_ID")+"/v1"),
		option.WithAPIKey(os.Getenv("VULNETIX_API_KEY")), // the Vulnetix key, not an OpenAI key
	)

	resp, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
		Model:    "gpt-4o-mini",
		Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("Hello")},
	})
	if err != nil {
		var apierr *openai.Error
		if errors.As(err, &apierr) && apierr.StatusCode == http.StatusForbidden {
			// apierr.Code is request_blocked, tool_denied, model_not_allowed, ...
			// apierr.RawJSON() is the error object, including blocked_by and violations.
			fmt.Fprintf(os.Stderr, "AI Firewall refused (%s): %s\n", apierr.Code, apierr.RawJSON())
			os.Exit(1)
		}
		panic(err)
	}
	fmt.Println(resp.Choices[0].Message.Content)
}
go run .

Streaming

stream := client.Chat.Completions.NewStreaming(context.Background(), openai.ChatCompletionNewParams{
	Model:    "gpt-4o-mini",
	Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("Hello")},
})
for stream.Next() {
	if chunk := stream.Current(); len(chunk.Choices) > 0 {
		fmt.Print(chunk.Choices[0].Delta.Content)
	}
}
if err := stream.Err(); err != nil {
	panic(err)
}

A refusal is decided before the stream opens, so it comes back from stream.Err() as the same *openai.Error.

Other providers and surfaces

  • Another chat provider: change the slug and model, for example /groq/<uuid>/v1 with llama-3.3-70b-versatile.
  • client.Responses.New(...) works against openai.
  • For the Anthropic surface use github.com/anthropics/anthropic-sdk-go with option.WithBaseURL("https://guardrails.vulnetix.com/anthropic/<uuid>") (no /v1) and option.WithAuthToken(os.Getenv("VULNETIX_API_KEY")).

Error codes: Errors.

Edit this pageSources: cli/pkg/aifirewall/templates/go-openai.tmpl, cli/pkg/aifirewall/snippet.go, ai-firewall/internal/api/codec.go, ai-firewall/internal/policy/engine.go, ai-firewall/website/content/docs/sdks/go.md
esc
↑ ↓ move↵ openSearches in your browser. A key like autonomy.max or a code like threshold_required jumps straight to it.