adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

InvocationContext.model_copy(deep=True) raises "TypeError: cannot pickle '_thread.lock' object"`

Open Strandor opened this issue 1 month ago • 1 comments

Describe the bug Trying to deep copy the invocation context results in TypeError: cannot pickle '_thread.lock' object

To Reproduce

class ParalellsAgentCollectedState(BaseAgent):
    researcher: LlmAgent
    questions: list[str]

    def __init__(
        self,
        name: str,
        researcher: LlmAgent,
        questions: list[str],
     ):
        super().__init__(
            name=name,
            researcher=researcher,
            questions=questions,
        )

    def _create_branch_ctx_for_sub_agent(
        self,
        agent: BaseAgent,
        question: str,
        invocation_context: InvocationContext,
    ) -> InvocationContext:
        """Create isolated branch for every sub-agent."""

        invocation_context = invocation_context.model_copy(
            deep=True # Here is where the error happens
        )
        branch_suffix = f'{agent.name}.{question}'
        invocation_context.branch = (
            f'{invocation_context.branch}.{branch_suffix}'
            if invocation_context.branch
            else branch_suffix
        )
        return invocation_context

   @override
    async def _run_async_impl(
       self, ctx: InvocationContext
    ) -> AsyncGenerator[Event, None]:
        for question in self.questions:
            sub_ctx = self._create_branch_ctx_for_sub_agent(
                self,
                question,
                ctx,
            )
            sub_ctx.session.state["current_question"] = question
            run = self.researcher.run_async(sub_ctx)
            async for event in run:
                yield event

Expected behavior I'd expect each agent in the paralells run to have it's own seperate state now.

Desktop (please complete the following information):

  • OS: macOS
  • Python version(python -V): Python 3.14.0
  • ADK version(pip show google-adk): adk, version 1.18.0

Model Information:

  • Are you using LiteLLM: Yes
  • Which model is being used(e.g. gemini-2.5-pro): gpt-5

Strandor avatar Nov 10 '25 16:11 Strandor

Hello @Strandor ,

This error suggests that the object cannot be copied because it attempted to create a duplicate. You can try creating a shallow copy by removing the deep=True argument, if that fits your use case. We also recommend updating to the latest version of ADK.

If the issue persists, please share more details about your goal, the steps to reproduce the error, and the relevant parts of your code where you define and call your agents.

llalitkumarrr avatar Dec 01 '25 09:12 llalitkumarrr