Go
The official `openai-go` SDK pointed at the gateway, with streaming and reading the refusal body.
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
chatprovider: change the slug and model, for example/groq/<uuid>/v1withllama-3.3-70b-versatile. client.Responses.New(...)works againstopenai.- For the Anthropic surface use
github.com/anthropics/anthropic-sdk-gowithoption.WithBaseURL("https://guardrails.vulnetix.com/anthropic/<uuid>")(no/v1) andoption.WithAuthToken(os.Getenv("VULNETIX_API_KEY")).
Error codes: Errors.