{"record":{"id":"03555fd9cfa2fa5a","repo":"microsoft/autogen","slug":"reflect-on-tool-use-produced-no-valid-text-respons-03555f","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/_code_executor_agent.py","lineNumber":869,"sourceCode":"        \"\"\"\n        all_messages = system_messages + await model_context.get_messages()\n        llm_messages = cls._get_compatible_context(model_client=model_client, messages=all_messages)\n\n        reflection_result: Optional[CreateResult] = None\n\n        if model_client_stream:\n            async for chunk in model_client.create_stream(llm_messages):\n                if isinstance(chunk, CreateResult):\n                    reflection_result = chunk\n                elif isinstance(chunk, str):\n                    yield ModelClientStreamingChunkEvent(content=chunk, source=agent_name)\n                else:\n                    raise RuntimeError(f\"Invalid chunk type: {type(chunk)}\")\n        else:\n            reflection_result = await model_client.create(llm_messages)\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        yield Response(\n            chat_message=TextMessage(","sourceCodeStart":851,"sourceCodeEnd":887,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py#L851-L887","documentation":"CodeExecutorAgent's reflection step requires a non-empty result with str content. This fires when the reflection CreateResult is missing or its content is not a plain string (e.g., a list of tool calls or None), meaning the model did not produce a usable text reflection.","triggerScenarios":"Reflection enabled on a CodeExecutorAgent with a model that keeps returning tool-call content instead of text, a client returning list-typed content, or an empty completion; structured-output settings converting content to non-str.","commonSituations":"Tool-heavy models that ignore the no-tools reflection prompt; local models without a reliable plain-text completion mode; misconfigured output_content_type.","solutions":["Turn off the reflection feature if a text reflection is not needed.","Use a model/client combination that reliably returns plain text when tools are disabled.","Review output_content_type / json_output settings that may force non-string content.","Upgrade client packages; older builds mishandled the reflection completion."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"info = getattr(model_client, \"model_info\", None) or {}\n# reflection needs plain-text completions; verify with a no-tools probe\nresult = await model_client.create([UserMessage(content=\"say ok\", source=\"user\")], tool_choice=\"none\")\nassert isinstance(result.content, str) and result.content, \"model not suited for reflection\"","typeGuard":"def is_text_result(result) -> bool:\n    return bool(result) and isinstance(result.content, str)","tryCatchPattern":"try:\n    async for ev in agent.on_messages_stream(msgs, ct):\n        ...\nexcept RuntimeError as e:\n    if \"no valid text response\" in str(e):\n        # disable reflection; tool results remain in context\n        ...\n    raise","preventionTips":["Probe the model once with tool_choice='none' before enabling reflection.","Prefer reflection-capable chat models (strong instruction following) for this flow."],"tags":["reflection","code-executor","model-client","content-type"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}