Skip to content
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6d9fb2e
fix(docs): correct .NET package references and versions in installati…
tejas-dharani Jun 2, 2025
d07898b
docs: add missing TextMessage import in custom agents documentation
tejas-dharani Jun 2, 2025
35c538b
Merge branch 'main' into main
tejas-dharani Jun 3, 2025
867804a
Merge branch 'main' into main
tejas-dharani Jun 7, 2025
00d5c9e
fix(agentchat_fastapi): handle WebSocket disconnect gracefully in app…
tejas-dharani Jun 8, 2025
a37d911
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 11, 2025
892bb44
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 13, 2025
35f07ba
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 14, 2025
854836d
Merge branch 'microsoft:main' into fix/broad-exception-handling-6280
tejas-dharani Jun 16, 2025
2dc9a22
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 19, 2025
7385703
Revert "fix(docs): correct .NET package references and versions in in…
tejas-dharani Jun 19, 2025
018631e
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 24, 2025
5f06e8d
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 25, 2025
8e734e6
Merge branch 'main' into fix/broad-exception-handling-6280
tejas-dharani Jun 26, 2025
5d34871
Merge branch 'main' into fix/broad-exception-handling-6280
ekzhu Jun 26, 2025
fd909b6
removed test_websocket_disconnect.py file as per review
tejas-dharani Jun 27, 2025
837b79a
Revert indentation changes to custom-agents.ipynb from 00d5c9e
tejas-dharani Jun 27, 2025
e47a1cf
removed changes not related to this pr
tejas-dharani Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions python/samples/agentchat_fastapi/app_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ async def chat(websocket: WebSocket):

# User input function used by the team.
async def _user_input(prompt: str, cancellation_token: CancellationToken | None) -> str:
data = await websocket.receive_json()
message = TextMessage.model_validate(data)
return message.content
try:
data = await websocket.receive_json()
message = TextMessage.model_validate(data)
return message.content
except WebSocketDisconnect:
# Client disconnected while waiting for input - this is the root cause of the issue
logger.info("Client disconnected while waiting for user input")
raise # Let WebSocketDisconnect propagate to be handled by outer try/except

try:
while True:
Expand Down Expand Up @@ -130,21 +135,33 @@ async def _user_input(prompt: str, cancellation_token: CancellationToken | None)
async with aiofiles.open(history_path, "w") as file:
await file.write(json.dumps(history))

except WebSocketDisconnect:
# Client disconnected during message processing - exit gracefully
logger.info("Client disconnected during message processing")
break
except Exception as e:
# Send error message to client
error_message = {
"type": "error",
"content": f"Error: {str(e)}",
"source": "system"
}
await websocket.send_json(error_message)
# Re-enable input after error
await websocket.send_json({
"type": "UserInputRequestedEvent",
"content": "An error occurred. Please try again.",
"source": "system"
})

try:
await websocket.send_json(error_message)
# Re-enable input after error
await websocket.send_json({
"type": "UserInputRequestedEvent",
"content": "An error occurred. Please try again.",
"source": "system"
})
except WebSocketDisconnect:
# Client disconnected while sending error - exit gracefully
logger.info("Client disconnected while sending error message")
break
except Exception as send_error:
logger.error(f"Failed to send error message: {str(send_error)}")
break

except WebSocketDisconnect:
logger.info("Client disconnected")
except Exception as e:
Expand All @@ -155,7 +172,12 @@ async def _user_input(prompt: str, cancellation_token: CancellationToken | None)
"content": f"Unexpected error: {str(e)}",
"source": "system"
})
except:
except WebSocketDisconnect:
# Client already disconnected - no need to send
logger.info("Client disconnected before error could be sent")
except Exception:
# Failed to send error message - connection likely broken
logger.error("Failed to send error message to client")
pass


Expand Down