Skip to content

Fails to save agent state: _SignInState is not JSON serializable #262

@hstahl

Description

@hstahl

Using Authorization and BlobStorage at the same time results in serialization errors. The _SignInState includes a Pydantic model of Activity which cannot be serialized with json.dumps.

Issue is with this line offering an Activity to be serialized by storage implementations:

I initialized the AgentApplication like in the auto-signin sample, except storage is a BlobStorage.

STORAGE = BlobStorage(
    BlobStorageConfig(...)
)

CONNECTION_MANAGER = MsalConnectionManager(**agents_sdk_config)

ADAPTER = CloudAdapter(connection_manager=CONNECTION_MANAGER)

AUTHORIZATION = Authorization(
    storage=STORAGE, connection_manager=CONNECTION_MANAGER, **agents_sdk_config
)

AGENT_APP = AgentApplication[TurnState](
    storage=STORAGE, adapter=ADAPTER, authorization=AUTHORIZATION, **agents_sdk_config
)

@AGENT_APP.activity(ActivityTypes.message, auth_handlers=["GRAPH"])
async def on_message(context: TurnContext, state: TurnState) -> None:
    ...

Stack trace when a message activity is received.

Traceback (most recent call last):
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/channel_adapter.py", line 242, in run_pipeline
    return await self.middleware_set.receive_activity_with_status(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        context, callback
        ^^^^^^^^^^^^^^^^^
    )
    ^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/middleware_set.py", line 56, in receive_activity_with_status
    return await self.receive_activity_internal(context, callback)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/middleware_set.py", line 66, in receive_activity_internal
    return await callback(context)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/agent_application.py", line 679, in on_turn
    await self._start_long_running_call(context, self._on_turn)
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/agent_application.py", line 865, in _start_long_running_call
    return await func(context)
           ^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/agent_application.py", line 701, in _on_turn
    ) = await self._auth._on_turn_auth_intercept(context, turn_state)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/oauth/authorization.py", line 302, in _on_turn_auth_intercept
    sign_in_response = await self._start_or_continue_sign_in(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        context, state, auth_handler_id
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/oauth/authorization.py", line 259, in _start_or_continue_sign_in
    await self._save_sign_in_state(context, sign_in_state)
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/app/oauth/authorization.py", line 157, in _save_sign_in_state
    await self._storage.write({key: state})
  File ".venv/lib/python3.13/site-packages/microsoft_agents/hosting/core/storage/storage.py", line 91, in write
    await gather(*[self._write_item(key, value) for key, value in changes.items()])
  File ".venv/lib/python3.13/site-packages/microsoft_agents/storage/blob/blob_storage.py", line 90, in _write_item
    item_rep_bytes = json.dumps(item_JSON).encode("utf-8")
                     ~~~~~~~~~~^^^^^^^^^^^
  File "~/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "~/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "~/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "~/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type Activity is not JSON serializable
  "microsoft-agents-authentication-msal>=0.6.0",
  "microsoft-agents-hosting-aiohttp>=0.6.0",
  "microsoft-agents-hosting-teams>=0.6.0",
  "microsoft-agents-storage-blob>=0.6.0",

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions