Repository navigation
fix(sdk): don't deep-copy the agent LLM in ask_agent - #5085
Conversation
Deep-copying the agent LLM while it has a call in flight fails because its telemetry holds the open span context manager. A shallow copy is enough: LLMRegistry.add() already gives the copy fresh metrics and telemetry. Fixes #5082
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||
|
🤖 OpenHands is reviewing this PR. Head commit: This comment was posted by an AI agent (OpenHands). |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was created by an AI agent (OpenHands) on behalf of the repository maintainers.
Verdict: APPROVE ✅
Clean, minimal bug fix. Removing deep=True from the model_copy() in ask_agent() is the correct fix — every other side-LLM in the SDK (condenser, judge, hooks, sub-agents) already uses shallow model_copy(), and LLMRegistry.add() → _ensure_independent_metrics() already detects shared metrics and calls reset_metrics(), which creates fresh Metrics and Telemetry instances. So the ask-agent LLM still gets independent metrics/telemetry without the crash.
The regression test is well-constructed: it patches _transport_call (the lowest-level transport), so the real completion() path runs including span opening, then calls ask_agent() from within the transport callback while the agent LLM call is in flight — reproducing the exact TypeError: cannot pickle 'generator' object from #5082.
Risk Assessment: 🟢 LOW — ask_agent is a sidebar Q&A feature that bypasses the conversation flow and doesn't affect agent reasoning, tool calling, or planning. No eval/benchmark impact.
Automated review used the wrong decision (APPROVED instead of COMMENT) and is dismissed. Findings are reposted as a comment.
|
GITHUB_REVIEW_POSTED This comment was posted by an AI agent (OpenHands). |
(cherry picked from commit 261d7e1)
HUMAN:
Fixing the ask_agent method. This was generating some error during the release of the sdk
AGENT:
Why
LocalConversation.ask_agent()crashes withTypeError: cannot pickle 'generator' objectwhen it is called whilerun()is waiting on the LLM in another thread. On its first call it deep-copies the agent LLM, and since #4836 the LLM'sTelemetryholds the open span context manager (_span_cm) for the duration of each call. This is what failsexamples/01_standalone_sdk/28_ask_agent_example.pyon every release PR since v1.46.0.Summary
deep=Truefrom theask_agentLLM copy. Every other side-LLM in the SDK (condenser, judge, hooks, task/delegate tools) is already a shallowmodel_copy(), andLLMRegistry.add()already detects the shared metrics and callsLLM.reset_metrics(), so the ask-agent LLM still gets its ownMetricsandTelemetry.ask_agentwhile the agent LLM call is in flight.Issue Number
Fixes #5082
How to Test
Fails on
mainwithTypeError: cannot pickle 'generator' object, passes with this change.tests/sdk/conversation(821 passed) and the registry/telemetry/span suites (94 passed) are green; pre-commit is clean.End-to-end, mirroring the example: a real
Conversation.run()in a background thread with a 3 s fakeLLM._transport_call, callingask_agentwhile the agent call is in flight and again afterrun()returns.Script
Video/Screenshots
uv run python e2e_ask_agent_threaded.pyonmain(4c89b68):On this branch:
Type
Notes
The example itself was not re-run against a real LLM here; it should go green in
Run Examples Scriptson the next release PR.🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:07c6d07-pythonRun
All tags pushed for this build
About Multi-Architecture Support
07c6d07-python) is a multi-arch manifest supporting both amd64 and arm6407c6d07-python-amd64) are also available if needed