infiniflow/ragflow · error · ValueError

Invalid base64 encoding: {str(e)}

Error message

Invalid base64 encoding: {str(e)}

What it means

ValueError raised by a Pydantic field_validator on CodeExecutionRequest.code_b64 in the sandbox executor's API models. The field must be valid base64; b64decode(v, validate=True) is run on input, and any exception (binascii.Error for bad characters/length, or non-str input) is converted into this message.

Source

Thrown at agent/sandbox/executor_manager/models/schemas.py:72

    artifacts: list[ArtifactItem] = []

    # Structured return value produced by main()
    result: Optional[ExecutionStructuredResult] = None


class CodeExecutionRequest(BaseModel):
    code_b64: str = Field(..., description="Base64 encoded code string")
    language: SupportLanguage = Field(default=SupportLanguage.PYTHON, description="Programming language")
    arguments: Optional[dict] = Field(default={}, description="Arguments")

    @field_validator("code_b64")
    @classmethod
    def validate_base64(cls, v: str) -> str:
        try:
            base64.b64decode(v, validate=True)
            return v
        except Exception as e:
            raise ValueError(f"Invalid base64 encoding: {str(e)}")

View on GitHub (pinned to 554fb1133a)

Solutions

  1. Encode the code before sending: base64.b64encode(code.encode('utf-8')).decode('ascii') and send the result as code_b64.
  2. If you used urlsafe_b64encode, switch to standard b64encode (or translate -_ to +/ and re-pad).
  3. Ensure padding ('=') is preserved and no newlines/whitespace are introduced in transit.

Example fix

# before
requests.post(url, json={"code_b64": "print('hi')"})

# after
import base64
requests.post(url, json={"code_b64": base64.b64encode("print('hi')".encode()).decode()})
Defensive patterns

Strategy: validation

Validate before calling

import base64, binascii

def is_valid_standard_b64(s: str) -> bool:
    try:
        base64.b64decode(s, validate=True)
        return True
    except (binascii.Error, ValueError):
        return False

# before POSTing:
assert is_valid_standard_b64(payload['code_b64'])

Type guard

import base64, binascii

def is_standard_base64(s: str) -> bool:
    try:
        base64.b64decode(s, validate=True)
        return True
    except (binascii.Error, ValueError):
        return False

Try / catch

try:
    resp = requests.post(executor_url, json=payload)
    resp.raise_for_status()
except requests.HTTPError as e:
    if 'Invalid base64 encoding' in e.response.text:
        payload['code_b64'] = base64.b64encode(raw_code.encode()).decode()
        resp = requests.post(executor_url, json=payload)

Prevention

When it happens

Trigger: POSTing to the sandbox executor API with code_b64 containing characters outside the base64 alphabet, wrong padding, or sending raw source code instead of base64-encoding it first. validate=True makes even stray whitespace/newlines fail.

Common situations: Client forgets base64.b64encode and posts plain code; URL-safe base64 (urlsafe_b64encode with -/_) sent where standard base64 is expected; padding stripped by a URL path segment; double-encoding mismatches between client and server versions.

Related errors


AI-assisted analysis of infiniflow/ragflow@554fb1133a (2026-08-15). Data as JSON: /api/errors/1256ec54098c42ba. Report an issue: GitHub.