{"record":{"id":"75a226060d1ec326","repo":"BoundaryML/baml","slug":"object-constructor-returned-nil-pointer","errorCode":null,"errorMessage":"object constructor returned nil pointer","messagePattern":"object constructor returned nil pointer","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"engine/language_client_go/baml_go/raw_objects/utils.go","lineNumber":127,"sourceCode":"\t\tKwargs: kwargs,\n\t}\n\n\tencodedArgs, err := proto.Marshal(&args)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal object constructor arguments: %w\", err)\n\t}\n\tcEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))\n\n\tcBuf := C.WrapCallObjectConstructor(cEncodedArgs, C.uintptr_t(len(encodedArgs)))\n\n\tcontent_bytes := C.GoBytes(unsafe.Pointer(cBuf.ptr), C.int32_t(cBuf.len))\n\tC.WrapFreeBuffer(cBuf) // Free the buffer after use\n\n\tif cBuf.len == 0 {\n\t\treturn nil, fmt.Errorf(\"failed to call object constructor\")\n\t}\n\tif cBuf.ptr == nil {\n\t\treturn nil, fmt.Errorf(\"object constructor returned nil pointer\")\n\t}\n\n\tvar content_holder cffi.InvocationResponse\n\terr = proto.Unmarshal(content_bytes, &content_holder)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal content bytes: %w\", err)\n\t}\n\tparsed, err := decodeObjectResponse(rt, &content_holder)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode object response: %w\", err)\n\t}\n\n\treturn parsed, nil\n}\n\nfunc destructor(object RawPointer) error {\n\tresult, err := CallMethod(object, \"~destructor\", nil)\n","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/raw_objects/utils.go#L109-L145","documentation":"NewRawObject checks the buffer returned by the native constructor call; if its length is nonzero but the pointer is nil, it reports that the object constructor returned a nil pointer. This is a defensive invariant check against the CFFI boundary — a non-empty claimed length with no backing memory. It indicates a broken or corrupted response from the native runtime.","triggerScenarios":"WrapCallObjectConstructor returning a cBuf with len > 0 but ptr == nil during NewRawObject (or its callers NewCollector, newMediaFromUrl, newMediaFromBase64, NewTypeBuilder). Practically only occurs with a malfunctioning/mismatched native runtime or memory corruption at the FFI boundary.","commonSituations":"Mixed versions of generated CFFI bindings and the native library; a buggy native build; unsafe memory reuse bugs surfacing as corrupted buffers. Extremely rare in normal usage.","solutions":["Upgrade the baml Go module so the CFFI wrappers and native library come from the same release, then clean-rebuild (go clean -cache).","Capture the exact object type and kwargs and file a bug with the BAML maintainers — this check guards against an internal invariant violation.","Rule out memory corruption in your own process: check for cgo misuse, unsafe casts, or concurrent calls into the runtime during init.","Test with a different platform/arch build of the native library to exclude a broken artifact."],"exampleFix":"// before\n// treating the empty/nil buffer as usable\nobj, err := NewRawObject(ctx, objectType, kwargs)\n// err: object constructor returned nil pointer\n\n// after\n// pin matching versions and rebuild natively\n// go.mod: require github.com/boundaryml/baml v0.XX.0\n// $ go clean -cache && go mod download && go build\n// if it persists, file a bug with the repro args","handlingStrategy":"try-catch","validationCode":"// No caller-side pre-check exists; guard versions instead:\n// go.mod pins baml module == native library release\nrequire github.com/boundaryml/baml v0.XX.0","typeGuard":"func responseUsable(ptr unsafe.Pointer, length int) bool {\n    return ptr != nil && length > 0\n}","tryCatchPattern":"obj, err := NewRawObject(ctx, objectType, kwargs)\nif err != nil && strings.Contains(err.Error(), \"nil pointer\") {\n    log.Printf(\"BAML FFI invariant violation; rebuild with matching versions and report bug\")\n    return err\n}","preventionTips":["Always build against a single, matching release of bindings + native library.","Avoid concurrent runtime calls during initialization.","Report this error with a repro to the maintainers — it signals an internal bug, not user error."],"tags":["cffi","native-runtime","null-pointer","invariant-violation"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}