{"record":{"id":"4a83bdafee608681","repo":"microsoft/autogen","slug":"reflect-on-tool-use-produced-no-valid-text-respons","errorCode":null,"errorMessage":"Reflect on tool use produced no valid text response.","messagePattern":"Reflect on tool use produced no valid text response\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py","lineNumber":1458,"sourceCode":"            ):\n                if isinstance(chunk, CreateResult):\n                    reflection_result = chunk\n                elif isinstance(chunk, str):\n                    yield ModelClientStreamingChunkEvent(\n                        content=chunk, source=agent_name, full_message_id=reflection_message_id\n                    )\n                else:\n                    raise RuntimeError(f\"Invalid chunk type: {type(chunk)}\")\n        else:\n            reflection_result = await model_client.create(\n                llm_messages,\n                json_output=output_content_type,\n                cancellation_token=cancellation_token,\n                tool_choice=\"none\",  # Do not use tools in reflection flow.\n            )\n\n        if not reflection_result or not isinstance(reflection_result.content, str):\n            raise RuntimeError(\"Reflect on tool use produced no valid text response.\")\n\n        # --- NEW: If the reflection produced a thought, yield it ---\n        if reflection_result.thought:\n            thought_event = ThoughtEvent(content=reflection_result.thought, source=agent_name)\n            yield thought_event\n            inner_messages.append(thought_event)\n\n        # Add to context (including thought if present)\n        await model_context.add_message(\n            AssistantMessage(\n                content=reflection_result.content,\n                source=agent_name,\n                thought=getattr(reflection_result, \"thought\", None),\n            )\n        )\n\n        if output_content_type:\n            content = output_content_type.model_validate_json(reflection_result.content)","sourceCodeStart":1440,"sourceCodeEnd":1476,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py#L1440-L1476","documentation":"After reflect_on_tool_use runs a follow-up model call (with tool_choice=\"none\"), AssistantAgent requires a non-empty result whose content is a plain string. This error fires when the reflection result is falsy or its content is not str (e.g., a list of FunctionCall, None, or structured output object).","triggerScenarios":"reflect_on_tool_use=True with a model/client that ignores tool_choice=\"none\" and returns tool calls anyway; a client returning content as a list; an empty completion; output_content_type (json_output) configured so reflection content is parsed into a non-str object.","commonSituations":"Models that are tool-call-heavy and keep emitting tool-call content even when tools are disabled; custom clients that don't honor tool_choice; JSON/structured-output mode returning dicts.","solutions":["Set reflect_on_tool_use=False if you don't need the reflection step.","Use a model/client that honors tool_choice=\"none\" and returns plain text completions.","If output_content_type is set, verify it is compatible with the reflection path or remove it.","Upgrade autogen-ext client packages; older clients sometimes drop tool_choice."],"exampleFix":"// before\nassistant = AssistantAgent(\n    name=\"assistant\",\n    model_client=client,\n    reflect_on_tool_use=True,\n)\n\n// after\nassistant = AssistantAgent(\n    name=\"assistant\",\n    model_client=client,\n    reflect_on_tool_use=False,\n)","handlingStrategy":"fallback","validationCode":"info = model_client.model_info or {}\nsupports_text = True  # verify client honors tool_choice='none'\nif reflect_on_tool_use and not supports_text:\n    reflect_on_tool_use = False","typeGuard":"def reflection_is_text(result) -> bool:\n    return bool(result) and isinstance(result.content, str) and len(result.content) > 0","tryCatchPattern":"try:\n    async for ev in assistant.on_messages_stream(msgs, ct):\n        ...\nexcept RuntimeError as e:\n    if \"no valid text response\" in str(e):\n        # rerun without reflection; tool results are already in context\n        assistant._reflect_on_tool_use = False\n    raise","preventionTips":["Test reflection once with your exact model before enabling it in production.","Avoid reflect_on_tool_use with models that persistently emit tool-call content.","Keep reflection off when output_content_type parses content into non-str objects."],"tags":["reflection","tool-use","model-client","tool-choice"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}