run-llama/llama_index · error · ValueError

Expected Response object, got {type(answer_obj)} instead.

Error message

Expected Response object, got {type(answer_obj)} instead.

What it means

FLAREQueryEngine decomposes generation into lookahead steps and calls self._query_engine.query(...) for each sub-question, then does `str(answer_obj)` and reads .source_nodes — both only exist on a (non-streaming) Response. If the wrapped query engine returns anything else (StreamingResponse, AgentOutput, a plain string engine, etc.), it raises ValueError with the offending type name.

Source

Thrown at llama-index-core/llama_index/core/query_engine/flare/base.py:223

            lookahead_resp = lookahead_resp.strip()
            if self._verbose:
                print_text(f"Lookahead response: {lookahead_resp}\n", color="pink")

            is_done, fmt_lookahead = self._done_output_parser.parse(lookahead_resp)
            if is_done:
                cur_response = cur_response.strip() + " " + fmt_lookahead.strip()
                break

            # parse lookahead response into query tasks
            query_tasks = self._query_task_output_parser.parse(lookahead_resp)

            # get answers for each query task
            query_tasks = query_tasks[: self._max_lookahead_query_tasks]
            query_answers = []
            for _, query_task in enumerate(query_tasks):
                answer_obj = self._query_engine.query(query_task.query_str)
                if not isinstance(answer_obj, Response):
                    raise ValueError(
                        f"Expected Response object, got {type(answer_obj)} instead."
                    )
                query_answer = str(answer_obj)
                query_answers.append(query_answer)
                source_nodes.extend(answer_obj.source_nodes)

            # fill in the lookahead response template with the query answers
            # from the query engine
            updated_lookahead_resp = self._lookahead_answer_inserter.insert(
                lookahead_resp, query_tasks, query_answers, prev_response=cur_response
            )

            # get "relevant" lookahead response by truncating the updated
            # lookahead response until the start position of the first tag
            # also remove the prefix from the lookahead response, so that
            # we can concatenate it with the existing response
            relevant_lookahead_resp_wo_prefix = self._get_relevant_lookahead_response(
                updated_lookahead_resp

View on GitHub (pinned to afd0fef371)

Solutions

  1. Give FLARE a plain RetrieverQueryEngine built without streaming: RetrieverQueryEngine.from_args(retriever=..., response_mode='compact') (streaming off by default).
  2. If you passed a custom engine, wrap it so query() returns Response(source_nodes=[...], response=...), or use llama_index.core.response.Response directly.
  3. Ensure any custom response synthesizer you inject into the inner engine is non-streaming (streaming=False).
  4. Check for the type before wiring: a one-off `type(inner.query('ping'))` in a scratch script confirms it is `llama_index.core.response.Response`.

Example fix

# before
inner = RetrieverQueryEngine.from_args(
    retriever=retriever,
    response_synthesizer=get_response_synthesizer(streaming=True),  # returns StreamingResponse
)
flare = FLAREQueryEngine(query_engine=inner)  # -> ValueError at first lookahead

# after
inner = RetrieverQueryEngine.from_args(
    retriever=retriever,
    response_mode="compact",  # non-streaming -> returns Response
)
flare = FLAREQueryEngine(query_engine=inner, retriever=retriever)
Defensive patterns

Strategy: validation

Validate before calling

from llama_index.core.response import Response

def assert_engine_returns_response(engine, probe: str = "ping") -> None:
    out = engine.query(probe)
    if not isinstance(out, Response):
        raise TypeError(
            f"FLARE requires a non-streaming Response; inner engine returned {type(out).__name__}. "
            "Rebuild with RetrieverQueryEngine.from_args(...) and streaming off."
        )

assert_engine_returns_response(inner_engine)  # before FLAREQueryEngine(...)

Type guard

from llama_index.core.response import Response, StreamingResponse

def returns_plain_response(engine) -> bool:
    out = engine.query("probe")
    return isinstance(out, Response) and not isinstance(out, StreamingResponse)

Prevention

When it happens

Trigger: Constructing FLAREQueryEngine(query_engine=...) with an inner engine configured for streaming (e.g. RetrieverQueryEngine with a streaming response_synthesizer), or with a custom/agent engine whose query() returns a non-Response object. The error surfaces on the first lookahead step, inside FLAREQueryEngine._query.

Common situations: Reusing an engine built for chat/streaming UX as FLARE's inner engine; passing a CustomQueryEngine (returns str) as query_engine; building the inner engine with RetrieverQueryEngine.from_args(..., streaming=True) or a response_mode that yields streaming responses.

Related errors


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