BoundaryML/baml · error

unexpected type for body: %T

Error message

unexpected type for body: %T

What it means

Thrown by HTTPRequest.Body() in BAML's Go client when the raw object returned by the FFI CallMethod("body") call cannot be asserted to the HTTPBody interface. It means the body payload decoded from the Rust core arrived as an unexpected Go type (%T names the actual type), indicating a version mismatch or corrupted/unsupported body encoding between the BAML core and the Go bindings.

Source

Thrown at engine/language_client_go/pkg/rawobjects_http_request.go:91

	}

	headers, ok := result.(map[string]string)
	if !ok {
		return nil, fmt.Errorf("unexpected type for headers: %T", result)
	}

	return headers, nil
}

func (h *httpRequest) Body() (HTTPBody, error) {
	result, err := raw_objects.CallMethod(h, "body", nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get body: %w", err)
	}

	body, ok := result.(HTTPBody)
	if !ok {
		return nil, fmt.Errorf("unexpected type for body: %T", result)
	}

	return body, nil
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Upgrade the Go bindings (engine/language_client_go) and the BAML core library to matching versions with 'go get -u github.com/boundaryml/baml/...'
  2. Print %T from the error message and check which concrete type arrived; inspect pkg/cffi decoders for a missing case
  3. Regenerate/refresh the baml generated client for your project so the core and bindings agree on object types
  4. If persistent, report the %T value to the BAML maintainers since it indicates an unhandled decoder path

Example fix

// before: blindly using the body
body, err := req.Body() // errors with unexpected type

// after: guard and surface the actual type
body, err := req.Body()
if err != nil {
    log.Printf("body unavailable: %v", err) // %T reveals the real type
    return err
}
Defensive patterns

Strategy: type-guard

Validate before calling

if req == nil { return errors.New("nil HTTPRequest") }

Type guard

func safeBody(req baml.HTTPRequest) (baml.HTTPBody, error) {
    b, err := req.Body()
    if err != nil {
        return nil, fmt.Errorf("body guard: %w", err)
    }
    return b, nil
}

Try / catch

body, err := req.Body()
if err != nil {
    var te *baml.UnexpectedTypeError // if exposed
    if errors.As(err, &te) { log.Printf("decoded type: see %T in message", err) }
    return fmt.Errorf("http body decode failed: %w", err)
}

Prevention

When it happens

Trigger: Calling baml HTTPRequest.Body() on a request object obtained from event logs or LLMCall.HttpRequest() when the underlying CFFI-decoded result is not registered as an HTTPBody implementation — e.g. the body was decoded as a generic RawObject or nil-typed wrapper instead of an httpBody.

Common situations: Mixed BAML Go-binding versions where the Go module is older/newer than the installed baml core library; reading event-log objects after a core upgrade changed body encoding; body objects whose type tag isn't recognized by the decoder.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/554081284ddb0ff9. Report an issue: GitHub.