BoundaryML/baml · error

failed to unmarshal content bytes: %w

Error message

failed to unmarshal content bytes: %w

What it means

This error wraps a protobuf unmarshal failure inside NewRawObject. After calling the native (Rust/C) object constructor over FFI, Go reads the returned byte buffer and tries to decode it as a cffi.InvocationResponse protobuf message; if the bytes are not a valid protobuf payload the construction fails. It indicates an FFI/IPC layer corruption or version mismatch rather than a problem with user arguments.

Source

Thrown at engine/language_client_go/baml_go/raw_objects/utils.go:133

	}
	cEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))

	cBuf := C.WrapCallObjectConstructor(cEncodedArgs, C.uintptr_t(len(encodedArgs)))

	content_bytes := C.GoBytes(unsafe.Pointer(cBuf.ptr), C.int32_t(cBuf.len))
	C.WrapFreeBuffer(cBuf) // Free the buffer after use

	if cBuf.len == 0 {
		return nil, fmt.Errorf("failed to call object constructor")
	}
	if cBuf.ptr == nil {
		return nil, fmt.Errorf("object constructor returned nil pointer")
	}

	var content_holder cffi.InvocationResponse
	err = proto.Unmarshal(content_bytes, &content_holder)
	if err != nil {
		return nil, fmt.Errorf("failed to unmarshal content bytes: %w", err)
	}
	parsed, err := decodeObjectResponse(rt, &content_holder)
	if err != nil {
		return nil, fmt.Errorf("failed to decode object response: %w", err)
	}

	return parsed, nil
}

func destructor(object RawPointer) error {
	result, err := CallMethod(object, "~destructor", nil)

	if err != nil {
		return fmt.Errorf("failed to call destructor: %w", err)
	}

	if result != nil {
		return fmt.Errorf("destructor returned unexpected result: %v", result)

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Rebuild/reinstall the BAML Go bindings so the Go cffi protobuf definitions match the native library version
  2. Check the wrapped %w error for the specific protobuf parse failure (e.g. 'proto: cannot parse invalid wire-format data')
  3. Set BAML_FFI_CLIENT_LOG to capture FFI logs and report the issue with them
  4. Retry the operation once to rule out transient memory corruption

Example fix

// before: mixed versions of native lib and Go module
go get github.com/boundaryml/baml@latest
// after: keep go module and native lib versions in lockstep
go.mod: require github.com/boundaryml/baml vX.Y.Z matching baml-cli vX.Y.Z
Defensive patterns

Strategy: try-catch

Validate before calling

if rt == nil { return fmt.Errorf("baml runtime must be initialized before creating objects") }

Try / catch

obj, err := b.NewCollector(rt)
if err != nil {
    if strings.Contains(err.Error(), "failed to unmarshal content bytes") {
        // FFI/protobuf corruption: rebuild bindings or fail fast
        return fmt.Errorf("baml FFI bridge corrupted: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling b.NewCollector(), b.NewTypeBuilder(), or a media constructor (newMediaFromUrl/newMediaFromBase64) when the native library returns a corrupted or incompatible byte buffer from WrapCallObjectConstructor.

Common situations: Mismatched BAML native library and Go bindings versions, memory corruption in the FFI bridge, or a truncated/zero-filled buffer being read after the buffer was freed incorrectly.

Understand the failure class

Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.

Related errors


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