Skip to content

Python: [Bug]: deepcopy fails on ChatAgent.run() when agent has tools - TypeError: cannot pickle '_thread.RLock' object #3368

@droideronline

Description

@droideronline

Description

When using ChatAgent with tools and calling agent.run() or agent.run_stream(), the method fails with a TypeError because _prepare_thread_and_messages calls deepcopy(self.default_options) which contains AIFunction objects that hold references to OpenTelemetry Histogram objects containing threading.Lock.

Code Sample

from agent_framework import ChatAgent, ai_function

@ai_function
def my_tool(param: str) -> str:
    """A simple tool."""
    return f"Result: {param}"

# Create agent with tools
agent = ChatAgent(
    chat_client=some_chat_client,
    name="test-agent",
    tools=[my_tool],  # Any tool causes the issue
)

# This will fail
response = await agent.run("Hello")

Error Messages / Stack Traces

Traceback (most recent call last):
  File ".../agent_framework/_agents.py", line 806, in run
    thread, run_chat_options, thread_messages = await self._prepare_thread_and_messages(
  File ".../agent_framework/_agents.py", line 1229, in _prepare_thread_and_messages
    chat_options = deepcopy(self.default_options) if self.default_options else {}
  File "/usr/lib/python3.11/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  ...
  File "/usr/lib/python3.11/copy.py", line 271, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib/python3.11/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/usr/lib/python3.11/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  ...
TypeError: cannot pickle '_thread.RLock' object

Package Versions

agent-framework v1.0.0b260116

Python Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

agentsIssues related to single agentsbugSomething isn't workingpythonv1.0Features being tracked for the version 1.0 GA

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions