{"record":{"id":"30cda30d393371c1","repo":"microsoft/semantic-kernel","slug":"failed-to-post-activity","errorCode":null,"errorMessage":"Failed to post activity.","messagePattern":"Failed to post activity\\.","errorType":"exception","errorClass":"AgentInvokeException","httpStatus":null,"severity":"error","filePath":"python/samples/demos/copilot_studio_agent/src/direct_line_agent.py","lineNumber":199,"sourceCode":"\n        # Step 2: Start a conversation if one hasn't already been started.\n        if not self.conversation_id:\n            start_conv_url = f\"{self.bot_endpoint}/conversations\"\n            async with self.session.post(start_conv_url, headers=headers) as resp:\n                if resp.status not in (200, 201):\n                    logger.error(\"Failed to start conversation. Status: %s\", resp.status)\n                    raise AgentInvokeException(\"Failed to start conversation.\")\n                conv_data = await resp.json()\n                self.conversation_id = conv_data.get(\"conversationId\")\n                if not self.conversation_id:\n                    raise AgentInvokeException(\"Conversation ID not found in start response.\")\n\n        # Step 3: Post the message payload.\n        activities_url = f\"{self.bot_endpoint}/conversations/{self.conversation_id}/activities\"\n        async with self.session.post(activities_url, json=payload, headers=headers) as resp:\n            if resp.status != 200:\n                logger.error(\"Failed to post activity. Status: %s\", resp.status)\n                raise AgentInvokeException(\"Failed to post activity.\")\n            _ = await resp.json()  # Response from posting activity is ignored.\n\n        # Step 4: Poll for new activities using watermark until DynamicPlanFinished event is found.\n        finished = False\n        collected_data = None\n        watermark = None\n        while not finished:\n            url = activities_url if watermark is None else f\"{activities_url}?watermark={watermark}\"\n            async with self.session.get(url, headers=headers) as resp:\n                if resp.status == 200:\n                    data = await resp.json()\n                    watermark = data.get(\"watermark\", watermark)\n                    activities = data.get(\"activities\", [])\n                    if any(\n                        activity.get(\"type\") == \"event\" and activity.get(\"name\") == \"DynamicPlanFinished\"\n                        for activity in activities\n                    ):\n                        collected_data = data","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/copilot_studio_agent/src/direct_line_agent.py#L181-L217","documentation":"An AgentInvokeException raised when posting the activity payload fails: POST {bot_endpoint}/conversations/{conversation_id}/activities returns a non-200 status. The posted activity is the user's message; if it isn't accepted the bot never receives it. Status is logged.","triggerScenarios":"POST activities with the JSON payload returns 4xx/5xx — e.g. 400 bad payload, 401 expired token, 404 unknown conversation_id, 429 rate limited.","commonSituations":"Payload schema invalid for the bot; conversation_id expired/invalid; token expired; rate limiting from rapid message bursts.","solutions":["Inspect logged status: 400 => payload; 401 => token; 404 => conversation; 429 => slow down.","Validate the payload from _build_payload matches the activity schema the bot expects.","Refresh the token/conversation if expired; implement backoff for 429."],"exampleFix":"// before\nif resp.status != 200:\n    raise AgentInvokeException(\"Failed to post activity.\")\n\n// after\nif resp.status != 200:\n    body = await resp.text()\n    raise AgentInvokeException(f\"Failed to post activity (status {resp.status}): {body}\")","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await post_activity(payload)\nexcept AgentInvokeException as e:\n    if \"Failed to post activity\" in str(e):\n        logger.warning(\"Activity post failed; will retry once\")\n        await asyncio.sleep(1); await post_activity(payload)\n    raise","preventionTips":["Validate the activity payload schema matches what the bot expects before posting.","Handle 429 with backoff to avoid burst failures."],"tags":["network","directline","copilot-studio","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}