{"record":{"id":"94bf90aa260ad9e4","repo":"BoundaryML/baml","slug":"encoding-internal-object-w","errorCode":null,"errorMessage":"encoding internal object: %w","messagePattern":"encoding internal object: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":92,"sourceCode":"\n\t// Handle Pointers: Dereference non-nil pointers for kind checks, but use original for interfaces\n\tif rv.Kind() == reflect.Ptr {\n\t\tif rv.IsNil() {\n\t\t\treturn &cffi.HostValue{}, nil // Treat nil pointers as nil values\n\t\t}\n\t\t// Work with the pointed-to value for subsequent kind checks\n\t\trv = rv.Elem()\n\t}\n\n\t// Handle concrete types (Checked, StreamState) before general kinds\n\t// Use the potentially dereferenced value 'rv.Interface()' here if concrete types are structs\n\tconcreteValue := rv.Interface() // Get the concrete value (dereferenced if original was pointer)\n\n\t// Check originalValue first (for non-pointer cases)\n\tif internalObject, ok := originalValue.(InternalBamlSerializer); ok {\n\t\thandle, err := internalObject.Encode()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding internal object: %w\", err)\n\t\t}\n\t\treturn &cffi.HostValue{\n\t\t\tValue: &cffi.HostValue_Handle{\n\t\t\t\tHandle: handle,\n\t\t\t},\n\t\t}, nil\n\t}\n\n\t// Also check the dereferenced value for pointer-to-interface cases (e.g., *types.Image)\n\t// In Go, a pointer to an interface doesn't implement the interface, but the dereferenced\n\t// value (the interface itself) does.\n\tif internalObject, ok := concreteValue.(InternalBamlSerializer); ok {\n\t\thandle, err := internalObject.Encode()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding internal object: %w\", err)\n\t\t}\n\t\treturn &cffi.HostValue{\n\t\t\tValue: &cffi.HostValue_Handle{","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L74-L110","documentation":"This error wraps a failure returned by an internal BAML object's Encode() method while serializing arguments for a BAML function call in the Go client. encodeValue detects values implementing InternalBamlSerializer (baml-internal runtime objects like Image, Audio, etc.) and asks them to produce a CFFI handle; when that internal encoding fails, the underlying cause is wrapped with this message and propagated to the caller of the baml function.","triggerScenarios":"Passing a baml-go internal object (e.g. types.Image, types.Audio, media wrappers implementing InternalBamlSerializer) directly or inside a list/map/class field to a generated baml function, when the object's Encode() fails — typically because the underlying media handle or runtime object was not properly constructed (e.g. FromURL/FromBase64 failed or the handle references an invalid resource).","commonSituations":"Constructing an Image/Audio from a bad URL or malformed base64 where construction partially succeeded; passing a zero-value internal object obtained from another call; embedding such objects in nested structs where the failure surfaces only at encode time.","solutions":["Inspect the wrapped cause (%w) in the error chain — it names the actual Encode() failure","Verify the internal object was constructed via its proper constructor (e.g. baml.Image.FromURL / FromBase64) and the URL/base64 is valid","Check that media URLs are reachable and base64 payloads are well-formed","Recreate the object rather than reusing one from a failed operation"],"exampleFix":"// before\nimg := types.Image{} // zero-value, no valid handle\nclassify(img)\n// after\nimg, err := types.Image.FromURL(\"https://example.com/photo.png\")\nif err != nil { return err }\nclassify(img)","handlingStrategy":"try-catch","validationCode":"func validMediaSrc(src string) bool { return strings.HasPrefix(src, \"http\") || isBase64(src) }","typeGuard":"func isInternalBaml(v any) bool { _, ok := v.(serde.InternalBamlSerializer); return ok }","tryCatchPattern":"if _, err := b.Fn(ctx, img); err != nil {\n  var wrapped interface{ Unwrap() error }\n  if errors.As(err, &target) { /* handle cause */ }\n  return fmt.Errorf(\"baml input encode failed: %w\", err)\n}","preventionTips":["Always construct internal media objects via their provided constructors (FromURL/FromBase64)","Check constructor errors immediately instead of reusing partially-built objects","Validate URLs/base64 before embedding media in baml inputs"],"tags":["go","serialization","baml"],"backgroundTag":"invalid-argument-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}