http-transforms

command
v1.3.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

HTTP Transforms Example

Customize HTTP tool behavior with argument transforms, request hooks, and response processing.

What You'll Learn

  • WithTransform() to map/normalize arguments before the HTTP request
  • WithPreRequest() to inject headers, correlation IDs, or dynamic auth
  • WithPostProcess() to transform responses after receiving
  • WithRedact() to strip sensitive fields before sending to the LLM

Prerequisites

  • Go 1.21+
  • OpenAI API key

Running the Example

export OPENAI_API_KEY=your-key
go run .

Code Overview

Argument Transform

Map LLM arguments to the format your API expects:

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/users",
    sdktools.WithTransform(func(args map[string]any) (map[string]any, error) {
        name, _ := args["name"].(string)
        parts := strings.SplitN(name, " ", 2)
        return map[string]any{
            "first_name": parts[0],
            "last_name":  parts[1],
        }, nil
    }),
)
conv.OnTool("lookup_user", cfg.Handler())
Pre-Request Hook

Modify the HTTP request before sending (auth, tracing, etc.):

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/orders",
    sdktools.WithPreRequest(func(req *http.Request) error {
        req.Header.Set("X-Correlation-ID", generateID())
        req.Header.Set("Authorization", "Bearer "+getToken())
        return nil
    }),
)
conv.OnToolCtx("get_orders", cfg.HandlerCtx())
Response Post-Processing + Redaction

Strip sensitive fields and add computed values:

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/customers",
    sdktools.WithRedact("ssn", "credit_card"),
    sdktools.WithPostProcess(func(resp []byte) ([]byte, error) {
        var data map[string]any
        json.Unmarshal(resp, &data)
        data["display_name"] = data["first_name"].(string) + " " + data["last_name"].(string)
        return json.Marshal(data)
    }),
)
conv.OnTool("get_customer", cfg.Handler())

Processing Pipeline

For each tool call, the pipeline runs in order:

  1. Transform - Normalize arguments (WithTransform)
  2. Pre-Request - Modify HTTP request (WithPreRequest)
  3. Execute - Send HTTP request
  4. Post-Process - Transform response (WithPostProcess)
  5. Redact - Strip sensitive fields (WithRedact)

Key Concepts

  1. Argument Mapping - Bridge the gap between LLM output and API input
  2. Dynamic Auth - Inject tokens at request time (OAuth refresh, etc.)
  3. Field Redaction - Prevent sensitive data from reaching the LLM
  4. Computed Fields - Enrich responses before the LLM sees them

Next Steps

Documentation

Overview

Package main demonstrates HTTP tool transforms and response processing via the SDK.

This example shows three patterns for HTTP tool customization:

  • WithTransform: normalize/map LLM arguments before the request
  • WithPreRequest: inject headers, tracing IDs, or auth at request time
  • WithPostProcess + WithRedact: transform responses and strip sensitive fields

Run with:

export OPENAI_API_KEY=your-key
go run .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL