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: QueryTypeView on GitHub (pinned to afd0fef371)
Solutions
- Coerce the query to str before calling select (e.g. query=str(user_input)).
- Or construct a QueryBundle explicitly: QueryBundle(query_str=...) when you also need extra fields (custom_embedding_str, metadata).
- 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
- Coerce external inputs to str before calling select.
- Type-annotate your wrapper functions so mypy catches bad query types.
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
- Unexpected type: {type(choice)}
- There are {len(self.reasons)} selections, please use .reason
- embeddings_cache must be of type BaseKVStore
- Invalid message content: {message.content!s}
- image_node.image is neither a string or None.
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/361db293ef9c1bc4.
Report an issue: GitHub.