Documentation
¶
Overview ¶
Package httputil provides HTTP transport and client utilities for LangChainGo.
The package offers several key features:
User-Agent Management ¶
All HTTP clients and transports in this package automatically add a User-Agent header that identifies the LangChainGo library, the calling program, and system information. This helps API providers understand client usage patterns and aids in debugging.
The User-Agent format is:
program/version langchaingo/version Go/version (GOOS GOARCH)
For example:
openai-chat-example/devel langchaingo/v0.1.8 Go/go1.21.0 (darwin arm64)
Default HTTP Client ¶
The package provides DefaultClient, which is a pre-configured http.Client that includes the User-Agent header:
resp, err := httputil.DefaultClient.Get("https://api.example.com/data")
Logging and Debugging ¶
For development and debugging, the package provides logging clients:
// LoggingClient logs full HTTP requests and responses using slog client := httputil.LoggingClient // JSONDebugClient pretty-prints JSON payloads and SSE streams with ANSI colors client := httputil.JSONDebugClient
Custom Transports ¶
The Transport type implements http.RoundTripper and can be used to add the LangChainGo User-Agent to any HTTP client:
client := &http.Client{
Transport: &httputil.Transport{
Transport: myCustomTransport,
},
}
Integration with httprr ¶
The transports in this package are designed to work with the httprr HTTP record/replay system used in tests. When using httprr, pass httputil.DefaultTransport to ensure proper request interception.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTransport is the default HTTP transport for LangChainGo. // It wraps [http.DefaultTransport] and adds a User-Agent header containing // the LangChainGo version, program information, and system details. // This transport is suitable for use with httprr in tests. DefaultTransport http.RoundTripper = &Transport{ Transport: http.DefaultTransport, } // DefaultClient is the default HTTP client for LangChainGo. // It uses [DefaultTransport] to automatically include proper User-Agent // headers in all requests. This client is recommended for all LangChainGo // HTTP operations unless custom transport behavior is required. DefaultClient = &http.Client{ Transport: DefaultTransport, } )
var DebugHTTPClient = LoggingClient
DebugHTTPClient is a deprecated alias for LoggingClient.
Deprecated: Use LoggingClient instead.
var DebugHTTPColorJSON = JSONDebugClient //nolint:gochecknoglobals
DebugHTTPColorJSON is a deprecated alias for JSONDebugClient.
Deprecated: Use JSONDebugClient instead.
var JSONDebugClient = &http.Client{ Transport: &Transport{ Transport: &jsonDebugTransport{}, }, }
JSONDebugClient is an http.Client designed for debugging JSON APIs and Server-Sent Events. It provides comprehensive debugging output including HTTP headers, JSON payloads, and real-time SSE event parsing. All debug output is written to stderr with ANSI colors: requests in blue, responses in green, SSE events in green, and parsed data in purple/yellow.
Key features:
- Pretty-prints JSON request and response bodies
- Displays HTTP headers with sensitive values scrubbed
- Streams SSE events in real-time as they arrive
- Parses token usage from streaming APIs
Unlike LoggingClient, this client writes directly to stderr rather than using structured logging.
var LoggingClient = &http.Client{ Transport: &Transport{ Transport: &LoggingTransport{}, }, }
LoggingClient is an http.Client that logs complete HTTP requests and responses using structured logging via slog. This client is useful for debugging API interactions, as it captures the full request and response including headers and bodies. The logs are emitted at DEBUG level.
Example:
slog.SetLogLoggerLevel(slog.LevelDebug)
resp, err := httputil.LoggingClient.Get("https://api.example.com/data")
Functions ¶
Types ¶
type ApiKeyTransport ¶
type ApiKeyTransport struct {
// Transport is the underlying [http.RoundTripper] to use.
// If nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
// APIKey is the API key to add to requests as a "key" query parameter.
APIKey string
}
ApiKeyTransport is an http.RoundTripper that adds API keys to URL query parameters. This is commonly used with Google APIs and other services that accept API keys as query parameters. It wraps another RoundTripper and automatically adds the API key if not already present in the request.
This transport is particularly useful when working with client libraries that don't properly set API keys when using custom HTTP clients, such as the Google AI client library when used with httprr for testing.
func (*ApiKeyTransport) RoundTrip ¶
RoundTrip implements the http.RoundTripper interface. It adds the API key as a "key" query parameter if not already present, then delegates to the underlying transport.
type LoggingTransport ¶
type LoggingTransport struct {
// Transport is the underlying [http.RoundTripper] to use.
// If nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
// Logger is the [slog.Logger] to use for logging.
// If nil, [slog.Default] is used.
Logger *slog.Logger
}
LoggingTransport is an http.RoundTripper that logs complete HTTP requests and responses using structured logging. It's designed for debugging and development purposes.
The transport logs at DEBUG level, so ensure your logger is configured appropriately to see the output.
func (*LoggingTransport) RoundTrip ¶
RoundTrip implements the http.RoundTripper interface. It logs the complete HTTP request (including headers and body) before sending it, executes the request using the underlying transport, then logs the complete response. Both request and response are logged at DEBUG level.
type Transport ¶
type Transport struct {
// Transport is the underlying [http.RoundTripper] to use.
// If nil, [http.DefaultTransport] is used.
Transport http.RoundTripper
}
Transport is an http.RoundTripper that adds LangChainGo User-Agent headers to outgoing HTTP requests. It wraps another RoundTripper (typically http.DefaultTransport) and can be used to add User-Agent headers to any HTTP client.
If the wrapped request already has a User-Agent header, the LangChainGo User-Agent is appended to it rather than replacing it.
func (*Transport) RoundTrip ¶
RoundTrip implements the http.RoundTripper interface. It adds the LangChainGo User-Agent header to the request and then delegates to the underlying transport. If the request already has a User-Agent header, the LangChainGo information is appended to preserve existing client identification.