run-llama/llama_index · error · ValueError

Could not format attribute {attribute_name} with value {temp

Error message

Could not format attribute {attribute_name} with value {template_str} to type {att_type}

What it means

Raised while formatting a templated media attribute (e.g. in ImageBlock/AudioBlock/VideoBlock.format or partial_format flows) when the formatted string cannot be converted to the field's declared type att_type. Only str and bytes targets are handled directly; other types go through att_type(formatted_str) and any exception triggers this error. Note: the message uses literal braces, so attribute names/types appear uninterpolated in the raised text.

Source

Thrown at llama-index-core/llama_index/core/base/llms/types.py:228

            # If the attribute is a binary string, we need to coerce to string for formatting,
            # but then we need to re-encode to bytes after formatting, which is what the code below does.
            formatted_kwargs = {
                k: resolve_binary(v, as_base64=True).read().decode()
                if isinstance(v, bytes)
                else v
                for k, v in kwargs.items()
            }
            if template_str:
                formatted_str = format_string(template_str, **formatted_kwargs)
                if att_type is str:
                    formatted_attrs[attribute_name] = formatted_str
                elif att_type is bytes:
                    formatted_attrs[attribute_name] = formatted_str.encode()
                else:
                    try:
                        formatted_attrs[attribute_name] = att_type(formatted_str)  # type: ignore
                    except Exception:
                        raise ValueError(
                            "Could not format attribute {attribute_name} with value {template_str} to type {att_type}"
                        )
        return type(self).model_validate(self.model_copy(update=formatted_attrs))

    @staticmethod
    def mimetype_from_inline_url(url: str) -> filetype.Type | None:
        if url.startswith("data:"):
            try:
                mimetype = url.split(";base64,")[0].split("data:")[1]
                return filetype.get_type(mime=mimetype)
            except Exception:
                try:
                    data = url.split(";base64,")[1]
                    decoded_data = base64.b64decode(data)
                    return filetype.guess(decoded_data)
                except Exception:
                    return None
        return None

View on GitHub (pinned to afd0fef371)

Solutions

  1. Inspect the block's attributes before formatting and ensure every template placeholder receives a value valid for the target type (e.g. full URL with scheme for URL-typed fields).
  2. Avoid templating non-str fields; resolve the value in your code and assign the final typed value directly instead of relying on format conversion.
  3. If a placeholder may be empty, give it a syntactically valid default (e.g. 'https://placeholder.local/x.png') or remove the attribute.

Example fix

# before
block = ImageBlock(url="{base}/img.png")
block.partial_format(base="")  # empty base -> invalid URL -> ValueError

# after
block = ImageBlock(url=f"{base}/img.png" if base else DEFAULT_IMAGE_URL)
Defensive patterns

Strategy: try-catch

Validate before calling

missing = [k for k in placeholder_names if k not in provided_kwargs]
if missing:
    raise ValueError(f"missing template values: {missing}")  # fail before formatting

Try / catch

try:
    block = block.partial_format(**kwargs)
except ValueError as e:
    if "Could not format attribute" in str(e):
        # resolve values manually and assign typed attributes directly
        block.url = HttpUrl(f"{base}/img.png")
    else:
        raise

Prevention

When it happens

Trigger: Calling block.partial_format(...) / format(...) on a block whose typed attribute template resolves to a value that its type constructor rejects, e.g. a URL field templated to a string that a stricter validator (like a Pydantic URL type) cannot parse.

Common situations: Templating prompt-time media attributes (paths, URLs) whose runtime substitutions produce empty or malformed values; partial_format leaving unresolved {placeholders} that the target type cannot parse.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/e201715628dca581. Report an issue: GitHub.