Skip to content

fix(sdk): don't deep-copy the agent LLM in ask_agent - #5085

Merged
VascoSch92 merged 1 commit into
mainfrom
vasco/fix-5082-ask-agent-deepcopy
Sep 15, 2026
Merged

VascoSch92 merged 1 commit into
mainfrom
vasco/fix-5082-ask-agent-deepcopy

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Fixing the ask_agent method. This was generating some error during the release of the sdk


AGENT:

Why

LocalConversation.ask_agent() crashes with TypeError: cannot pickle 'generator' object when it is called while run() is waiting on the LLM in another thread. On its first call it deep-copies the agent LLM, and since #4836 the LLM's Telemetry holds the open span context manager (_span_cm) for the duration of each call. This is what fails examples/01_standalone_sdk/28_ask_agent_example.py on every release PR since v1.46.0.

Summary

  • Drop deep=True from the ask_agent LLM copy. Every other side-LLM in the SDK (condenser, judge, hooks, task/delegate tools) is already a shallow model_copy(), and LLMRegistry.add() already detects the shared metrics and calls LLM.reset_metrics(), so the ask-agent LLM still gets its own Metrics and Telemetry.
  • Add a regression test that calls ask_agent while the agent LLM call is in flight.

Issue Number

Fixes #5082

How to Test

uv run pytest tests/sdk/conversation/test_ask_agent.py::test_ask_agent_during_in_flight_llm_call

Fails on main with TypeError: 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 fake LLM._transport_call, calling ask_agent while the agent call is in flight and again after run() returns.

Script
import threading, time
from unittest import mock
from litellm.types.utils import Choices, Message as LiteLLMMessage, ModelResponse, Usage
from pydantic import SecretStr
from openhands.sdk import LLM, Agent, Conversation

in_flight = threading.Event()

def fake_transport(llm, *args, **kwargs):
    if llm.usage_id != "ask-agent-llm":
        in_flight.set()
        time.sleep(3)
    return ModelResponse(
        id="x", created=0, model="gpt-4o-mini", object="chat.completion",
        choices=[Choices(finish_reason="stop", index=0, message=LiteLLMMessage(content=f"reply from {llm.usage_id}", role="assistant"))],
        usage=Usage(prompt_tokens=10, completion_tokens=5, total_tokens=15),
    )

llm = LLM(usage_id="agent", model="gpt-4o-mini", api_key=SecretStr("x"))
conversation = Conversation(agent=Agent(llm=llm, tools=[]), workspace=".", max_iteration_per_run=5)
conversation.send_message("Explore the current directory and describe the architecture")

with mock.patch.object(LLM, "_transport_call", autospec=True, side_effect=fake_transport):
    thread = threading.Thread(target=conversation.run)
    thread.start()
    in_flight.wait(5)
    print("agent LLM call in flight:", in_flight.is_set(), "| span held:", llm.telemetry._span_cm is not None)
    print("ask_agent during run():", conversation.ask_agent("Summarize the activity so far in 1 sentence."))
    thread.join()
    print("ask_agent after run():", conversation.ask_agent("Have you finished running?"))

ask_llm = conversation.llm_registry.get("ask-agent-llm")
print("independent telemetry:", ask_llm.telemetry is not llm.telemetry, "| independent metrics:", ask_llm.metrics is not llm.metrics)
print("status:", conversation.state.execution_status)

Video/Screenshots

uv run python e2e_ask_agent_threaded.py on main (4c89b68):

agent LLM call in flight: True | span held: True
TypeError: cannot pickle 'generator' object

On this branch:

agent LLM call in flight: True | span held: True
ask_agent during run(): reply from ask-agent-llm
ask_agent after run(): reply from ask-agent-llm
independent telemetry: True | independent metrics: True
status: ConversationExecutionStatus.FINISHED

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

The example itself was not re-run against a real LLM here; it should go green in Run Examples Scripts on 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:07c6d07-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-07c6d07-python \
  ghcr.io/openhands/agent-server:07c6d07-python

All tags pushed for this build

ghcr.io/openhands/agent-server:07c6d07-golang-amd64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-golang-amd64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-golang-amd64
ghcr.io/openhands/agent-server:07c6d07-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:07c6d07-golang-arm64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-golang-arm64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-golang-arm64
ghcr.io/openhands/agent-server:07c6d07-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:07c6d07-java-amd64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-java-amd64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-java-amd64
ghcr.io/openhands/agent-server:07c6d07-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:07c6d07-java-arm64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-java-arm64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-java-arm64
ghcr.io/openhands/agent-server:07c6d07-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:07c6d07-python-amd64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python-amd64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python-amd64
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime-amd64
ghcr.io/openhands/agent-server:07c6d07-python-arm64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python-arm64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python-arm64
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime-arm64
ghcr.io/openhands/agent-server:07c6d07-python-slim-amd64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python-slim-amd64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python-slim-amd64
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:07c6d07-python-slim-arm64
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python-slim-arm64
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python-slim-arm64
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:07c6d07-golang
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-golang
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-golang
ghcr.io/openhands/agent-server:07c6d07-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:07c6d07-java
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-java
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-java
ghcr.io/openhands/agent-server:07c6d07-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:07c6d07-python-slim
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python-slim
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python-slim
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime-slim
ghcr.io/openhands/agent-server:07c6d07-python
ghcr.io/openhands/agent-server:07c6d074d6135d50551e0d852c51336129a198e4-python
ghcr.io/openhands/agent-server:vasco-fix-5082-ask-agent-deepcopy-python
ghcr.io/openhands/agent-server:07c6d07-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 07c6d07-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 07c6d07-python-amd64) are also available if needed

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
@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/conversation/impl
   local_conversation.py10988792%168, 346, 412, 679–680, 722, 764, 1092, 1116–1117, 1122, 1137, 1139, 1243, 1259, 1307, 1333, 1413, 1417–1423, 1488, 1508–1510, 1559, 1578–1580, 1891–1892, 2130, 2133–2134, 2159, 2190, 2196, 2331, 2335, 2338, 2341, 2345–2346, 2350–2351, 2354, 2361, 2381, 2385, 2388, 2407, 2459, 2462, 2494, 2501–2502, 2510, 2514–2516, 2523, 2560–2566, 2569, 2572, 2579, 2680, 2685, 2805–2806, 2824–2825, 2858, 3063, 3067, 3137, 3144–3145
TOTAL427101191972% 

@VascoSch92
VascoSch92 marked this pull request as ready for review September 15, 2026 19:21
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🤖 OpenHands is reviewing this PR.

Head commit: 07c6d074d6135d50551e0d852c51336129a198e4
View the conversation: https://oss-agent-canvas.ngrok.dev/conversations/55935273-9d4c-44a4-a755-50b28d5953e6

This comment was posted by an AI agent (OpenHands).

all-hands-bot
all-hands-bot previously approved these changes Sep 15, 2026

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@all-hands-bot
all-hands-bot dismissed their stale review September 15, 2026 19:26

Automated review used the wrong decision (APPROVED instead of COMMENT) and is dismissed. Findings are reposted as a comment.

@all-hands-bot

Copy link
Copy Markdown
Collaborator

GITHUB_REVIEW_POSTED

This comment was posted by an AI agent (OpenHands).

@juanmichelini
juanmichelini self-requested a review September 15, 2026 19:31

@juanmichelini juanmichelini left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@VascoSch92
VascoSch92 merged commit 261d7e1 into main Sep 15, 2026
58 of 59 checks passed
@VascoSch92
VascoSch92 deleted the vasco/fix-5082-ask-agent-deepcopy branch September 15, 2026 19:52
VascoSch92 added a commit that referenced this pull request Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ask_agent() crashes with "cannot pickle 'generator' object" when called during an in-flight LLM call

3 participants