BoundaryML/baml · error
failed to call object constructor
Error message
failed to call object constructor
What it means
After marshaling, NewRawObject invokes WrapCallObjectConstructor in the native runtime, which returns a buffer; this error is raised when that buffer has zero length, meaning the native object-constructor call produced no response. It signals the CFFI call itself failed or returned an empty result rather than a structured error.
Source
Thrown at engine/language_client_go/baml_go/raw_objects/utils.go:124
func NewRawObject(rt unsafe.Pointer, objectType cffi.BamlObjectType, kwargs []*cffi.HostMapEntry) (any, error) {
args := cffi.BamlObjectConstructorInvocation{
Type: objectType,
Kwargs: kwargs,
}
encodedArgs, err := proto.Marshal(&args)
if err != nil {
return nil, fmt.Errorf("failed to marshal object constructor arguments: %w", err)
}
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
}
View on GitHub (pinned to bd85ce9dee)
Solutions
- Verify the BAML runtime is fully initialized before constructing raw objects (check init/order of runtime setup).
- Confirm the object type string and kwargs match the current BAML API for your version — upgrade Go bindings and native library together.
- Check stderr/logs from the native runtime for an abort or panic during the constructor call.
- Reduce the call to a minimal reproduction (simple object type, empty kwargs) to isolate whether the runtime itself is broken.
Example fix
// before
obj, err := NewRawObject(ctx, unknownType, kwargs) // empty response buffer
// after
if err := bamlRuntime.WaitReady(ctx); err != nil {
return fmt.Errorf("runtime init: %w", err)
}
obj, err := NewRawObject(ctx, knownSupportedType, kwargs)
if err != nil {
return fmt.Errorf("raw object: %w", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
if err := bamlRuntime.WaitReady(ctx); err != nil {
return fmt.Errorf("runtime not ready: %w", err)
} Type guard
func runtimeReady() bool {
return bamlRuntime.State() == ready
} Try / catch
obj, err := NewRawObject(ctx, objectType, kwargs)
if err != nil && strings.Contains(err.Error(), "failed to call object constructor") {
return fmt.Errorf("native constructor returned no response for %s — verify runtime init and object type: %w", objectType, err)
} Prevention
- Initialize the BAML runtime fully before any object construction.
- Use object type names valid for your installed BAML version.
- Upgrade Go bindings and native library together; watch native stderr for aborts.
When it happens
Trigger: WrapCallObjectConstructor returning an empty buffer during NewRawObject (or NewCollector / newMediaFromUrl / newMediaFromBase64 / NewTypeBuilder). This happens when the native runtime rejects or aborts the constructor call without producing a response payload — e.g. an unknown object type or a runtime not properly initialized.
Common situations: Mismatch between the Go bindings' object type names and the native runtime's supported types after partial upgrades; calling BAML object APIs before runtime initialization completed; native runtime crash/abort producing an empty buffer.
Related errors
- failed to marshal object constructor arguments: %w
- object constructor returned nil pointer
- value record mixed run_started with run_completed lifecycle
- value record mixed body metadata with lifecycle metadata
- value record mixed capture loss metadata with body metadata
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/ac152b235b23a961.
Report an issue: GitHub.