Skip to content

Orphaned function_call blocks from user interruption cause 400 errors #5856

Closed

Description

@seanxiong1989

Description

When a user sends a new message while a tool call is still pending (or the tool errors without ADK emitting a function_response), the persisted session contains a tool_use event with no matching tool_result. Claude's API requires every tool_use to have a corresponding tool_result — without one, every subsequent turn returns a 400 error and the session is permanently broken.

Difference from existing fixes

This is distinct from the fixes in #3504 and #4027, which address orphaned function_response blocks during context truncation. This issue is about orphaned function_call blocks caused by user interruption — a different problem that ADK does not currently handle.

Current workaround

We use a before_model_callback that scans llm_request.contents and injects synthetic function_response stubs for any unmatched function_call blocks, preserving the rest of the conversation context.

Expected behavior

ADK should handle orphaned function_call blocks (from interruption or tool errors) gracefully, either by injecting synthetic responses or by stripping the orphaned calls before sending to the model API.

Activity

  1. added
    core[Component] This issue is related to the core interface and implementation
    on May 26, 2026
  2. added theissue type on May 26, 2026
  3. llalitkumarrr commented on May 27, 2026

    @llalitkumarrr
    Collaborator

    Hello @seanxiong1989 ,

    Could you please share a minimal reproducible code and the steps for reproduction? Also are you facing this issue with Claude only or is a similar problem observed with other models? Furthermore, if you are not already using the latest version, kindly try our most recent release and share your observations.

  4. added
    request clarification[Status] The maintainer need clarification or more information from the author
    on May 27, 2026
  5. seanxiong1989 commented on May 27, 2026

    @seanxiong1989
    Author

    Hey @llalitkumarrr — reproduces on both 1.29.0 and 2.1.0. Claude-specific (Gemini doesn't enforce tool_use/tool_result pairing).

    Repro (Claude on Vertex AI):

    """Minimal reproduction: orphaned function_call permanently breaks session."""
    
    from __future__ import annotations
    import asyncio
    
    from google.adk.agents import Agent
    from google.adk.events import Event
    from google.adk.models.anthropic_llm import Claude
    from google.adk.models.registry import LLMRegistry
    from google.adk.runners import Runner
    from google.adk.sessions import InMemorySessionService
    from google.genai import types
    
    LLMRegistry.register(Claude)
    
    
    def get_weather(city: str) -> dict:
        """Look up weather for a city."""
        return {"city": city, "temp_f": 72, "condition": "sunny"}
    
    
    agent = Agent(
        name="repro_agent",
        model="claude-sonnet-4-6",
        instruction="You are a helpful assistant. Use the get_weather tool when asked about weather.",
        tools=[get_weather],
    )
    
    session_service = InMemorySessionService()
    runner = Runner(agent=agent, session_service=session_service, app_name="repro")
    
    
    async def main():
        session = await session_service.create_session(app_name="repro", user_id="user1")
        sid = session.id
    
        # Turn 1: normal conversation
        msg1 = types.Content(role="user", parts=[types.Part(text="What is the weather in NYC?")])
        async for event in runner.run_async(user_id="user1", session_id=sid, new_message=msg1):
            pass
    
        # Inject orphaned function_call (simulates user interruption mid-tool)
        session = await session_service.get_session(app_name="repro", user_id="user1", session_id=sid)
        orphan = Event(
            author="repro_agent",
            invocation_id="interrupted",
            content=types.Content(
                role="model",
                parts=[types.Part(function_call=types.FunctionCall(
                    id="orphan_001", name="get_weather", args={"city": "London"},
                ))],
            ),
        )
        await session_service.append_event(session, orphan)
    
        # Turn 2: this 400s, session is permanently broken
        msg2 = types.Content(role="user", parts=[types.Part(text="What about London?")])
        async for event in runner.run_async(user_id="user1", session_id=sid, new_message=msg2):
            pass
    
    
    if __name__ == "__main__":
        asyncio.run(main())
    export GOOGLE_CLOUD_PROJECT=<your-project>
    export GOOGLE_CLOUD_LOCATION=<region>
    python repro.py
    

    Output:

    Turn 2: ERROR: BadRequestError: messages.4: `tool_use` ids were found
    without `tool_result` blocks immediately after: orphan_001.
    Session is permanently broken.
    
  6. llalitkumarrr commented on May 29, 2026

    @llalitkumarrr
    Collaborator

    Hello @seanxiong1989 ,

    Thank you for sharing this detailed report.
    As you noted, this behavior is due to Claude's strict implementation of tool use and tool results pairing. We recommend that the best approach right now is to use the callback feature, which it appears you are already doing.

    Could you please let us know if callback solves this issue?

  7. adk-bot commented on Jun 5, 2026

    @adk-bot
    Collaborator

    This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

  8. added
    stale[Status] Issues which have been marked inactive since there is no user response
    on Jun 5, 2026
  9. adk-bot commented on Jun 12, 2026

    @adk-bot
    Collaborator

    This has been automatically closed because it has been marked as stale for over 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementationrequest clarification[Status] The maintainer need clarification or more information from the authorstale[Status] Issues which have been marked inactive since there is no user response

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions