Skip to content

fix: base64 encode returns bytes instead of string, causing serialization crashes #21186

@chinhkrb113

Description

@chinhkrb113

Description

base64.b64encode() returns a bytes object (e.g., b"Zm9v"). The function uses typing.cast(str, ...) which only satisfies the static type checker but does not actually convert the bytes to a string at runtime. Because the function signature advertises a str return type, downstream code will likely attempt to concatenate it with other strings (e.g., f"data:image/jpeg;base64,{img}") or serialize it to JSON for API requests. This will result in runtime errors like TypeError: Object of type bytes is not JSON serializable.

Severity: high
File: llama-index-core/llama_index/core/img_utils.py

Expected Behavior

The code should handle this case properly to avoid unexpected errors or degraded quality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions