run-llama/llama_index · error · ValueError

Unexpected type: {type(query)}

Error message

Unexpected type: {type(query)}

What it means

Raised by _wrap_query when the query argument to BaseSelector.select is neither a QueryBundle nor a str. The helper normalizes the query into a QueryBundle; any other type (int, dict, list, None) is rejected.

Source

Thrown at llama-index-core/llama_index/core/base/base_selector.py:69

SelectorResult = MultiSelection


def _wrap_choice(choice: MetadataType) -> ToolMetadata:
    if isinstance(choice, ToolMetadata):
        return choice
    elif isinstance(choice, str):
        return ToolMetadata(description=choice)
    else:
        raise ValueError(f"Unexpected type: {type(choice)}")


def _wrap_query(query: QueryType) -> QueryBundle:
    if isinstance(query, QueryBundle):
        return query
    elif isinstance(query, str):
        return QueryBundle(query_str=query)
    else:
        raise ValueError(f"Unexpected type: {type(query)}")


class BaseSelector(PromptMixin, DispatcherSpanMixin):
    """Base selector."""

    def _get_prompt_modules(self) -> PromptMixinType:
        """Get prompt sub-modules."""
        return {}

    def select(
        self, choices: Sequence[MetadataType], query: QueryType
    ) -> SelectorResult:
        metadatas = [_wrap_choice(choice) for choice in choices]
        query_bundle = _wrap_query(query)
        return self._select(choices=metadatas, query=query_bundle)

    async def aselect(
        self, choices: Sequence[MetadataType], query: QueryType

View on GitHub (pinned to afd0fef371)

Solutions

  1. Coerce the query to str before calling select (e.g. query=str(user_input)).
  2. Or construct a QueryBundle explicitly: QueryBundle(query_str=...) when you also need extra fields (custom_embedding_str, metadata).
  3. Add an input validation layer at your API boundary that rejects/normalizes non-string queries.

Example fix

# before
result = selector.select(choices=choices, query=request.json["q"])

# after
q = str(request.json["q"])
result = selector.select(choices=choices, query=q)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(query, (str, QueryBundle)):
    query = str(query)

Type guard

def is_valid_query(q: Any) -> bool:
    return isinstance(q, (str, QueryBundle))

Prevention

When it happens

Trigger: Calling selector.select(choices=..., query=<non-str-non-QueryBundle>) such as query=123, query=None, or query=some_dict. Also happens when a wrapper passes through an unvalidated user input.

Common situations: Passing a raw user-supplied payload (e.g. from a web request body) straight into a router's selector without coercing to str; refactors that changed the query variable's type.

Related errors


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