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

g_ui_adk.event_translator:Error translating ADK event: Object of type SecuritySchemeType is not JSON serializable

Open ldessemon2 opened this issue 1 year ago • 4 comments

Bug Description: TypeError: Object of type SecuritySchemeType is not JSON serializable

Context

Copilotkit.ai + ADK

Summary

Two issues are observed when the agent uses an OpenAPIToolset configured for OAuth2:

  1. An ERROR occurs during the event translation of a tool call, specifically a TypeError: Object of type SecuritySchemeType is not JSON serializable. This suggests an attempt to JSON-serialize internal security scheme objects when preparing tool call arguments for the UI.
  2. Following the authorization prompt response, a critical UI error is logged: CopilotError: Cannot send 'RUN_FINISHED' while tool calls are still active: <tool-call-id>. This indicates a state management issue where the framework believes a previous, long-running tool call is still active, preventing the session from cleanly progressing.

Reproduction Steps

  1. Environment Setup: Create an agent using the ADK framework.
  2. Tool Configuration: Configure an OpenAPIToolset with an OAuth2Auth credential.
    • The OpenAPIToolset is initialized with a spec_str containing an OAuth2 security scheme and an auth_credential of type OAuth2Auth.
    • The agent's tools list includes this openapi_toolset. (See openapi_connector_tool.py and agent.py in the provided context).
  3. Initial Conversation: Start a chat session and trigger the agent to call the tool (e.g., by asking to list workbooks). This will be the initial, unauthenticated call.
    • User Input 1: "list workbooks for site 'mysite'"
  4. Observation 1 (Error 1): The agent performs a tool call. After the call, the following Python traceback/log error is observed in the agent logs:
    [agent] ERROR:ag_ui_adk.event_translator:Error translating ADK event: Object of type SecuritySchemeType is not JSON serializable
    [agent] Traceback (most recent call last):
    ...
    [agent] TypeError: Object of type SecuritySchemeType is not JSON serializable
    
  5. User Interface: The UI prompts the user for authorization (a common response when a tool is called but no credentials are yet present for a long-running tool).
  6. Observation 2 (Error 2): After the authorization flow is complete, the UI logs this error:
    [ui] [21:44:05.280] ERROR: Error in action execution argument stream
    ...
    [ui]       "message": "Cannot send 'RUN_FINISHED' while tool calls are still active: adk-68111be6-b3ba-4ed5-9601-1277e4de4392",
    

Expected Behavior

  1. The ag_ui_adk.event_translator should correctly handle the serialization of tool call arguments, even when the underlying tool is an OpenAPIToolset with an OAuth2 security scheme, and should not attempt to serialize framework-specific objects like SecuritySchemeType.
  2. Upon completion of the initial, unauthenticated tool call (which prompts for authorization), the ADK framework should properly close the active tool call so that subsequent events (such as the successful authentication redirect or a second user prompt) can proceed without the "tool calls are still active" error.

Root Cause Suggestion

The ag_ui_adk/event_translator.py logic around line 338 attempts to json.dumps(func_call.args). If func_call.args contains an object from the fastapi.openapi.models module (like SecuritySchemeType which might be inadvertently packaged with the arguments during the initial tool call preparation), it fails serialization.

The second issue suggests a failure in the event lifecycle to correctly register and deregister the initial unauthenticated tool call, which is a required precursor for the OAuth flow. This tool call is registered as "long-running" but never appears to complete cleanly in the agent's state machine.

Relevant Code Snippets (from provided context)

openapi_connector_tool.py (Tool Initialization)

# The error happens when the tool is called and auth is missing
openapi_toolset = OpenAPIToolset(
        spec_str=OPENAPI_SPEC_YAML,
        spec_str_type="yaml",
        #auth_scheme=oauth2_scheme, 
        auth_credential=oauth2_credential
    )

Other comments

The same agent works well through "adk web"

ldessemon2 avatar Oct 26 '25 21:10 ldessemon2

I'm also hitting this as well even without an OpenAPIToolset. It seems to me that ADK + Auth +AG-UI just doesn't work at all.

rcleveng avatar Nov 16 '25 15:11 rcleveng

@ldessemon2 The issue is in ag-ui's event_translator.py around line 338 where it serializes tool arguments - SecuritySchemeType enums from fastapi can't be JSON serialized directly.

The fix is to use model_dump(mode='json') instead of model_dump() when calling json.dumps, or just use model_dump_json() directly.

This is tracked in CopilotKit/CopilotKit#2725.

For now, adk web works fine as a workaround since it doesn't have this serialization issue.

Thanks!

surajksharma07 avatar Dec 12 '25 10:12 surajksharma07

@seanzhou1023 Please have a look into it.

surajksharma07 avatar Dec 12 '25 10:12 surajksharma07

This is a CopilotKit/ag-ui issue, not an ADK bug — the error occurs in ag_ui_adk/event_translator.py (CopilotKit code), and adk web works fine with the same agent.

ADK's rest_api_tool.py and auth_helpers.py properly handle auth scheme serialization.

The fix is tracked in CopilotKit/CopilotKit#2725.

Closing on the ADK side as this is an external integration issue.

Please follow up in the CopilotKit repo.

surajksharma07 avatar Feb 06 '26 07:02 surajksharma07