Skip to content

[Bug] ConversationState fails to restore when secrets are serialized without cipher/expose_secrets #1671

Closed as duplicate

Description

@xingyaoww

Bug Description

When a ConversationState is persisted and later restored, the deserialization fails with a Pydantic validation error if the secrets in secret_registry.secret_sources were serialized without a cipher or expose_secrets=True context.

Root Cause

The issue is in the secret serialization/deserialization flow:

  1. When ConversationState._save_base_state() is called, it uses model_dump_json(exclude_none=True) without providing a cipher or expose_secrets context
  2. The serialize_secret() function in utils/pydantic_secrets.py returns the raw SecretStr object when no cipher/expose_secrets is provided (line 28)
  3. Pydantic then serializes this as "**********" (default masking)
  4. When deserializing, validate_secret() sees "**********" and returns None (line 52: if secret_value == "**********": return None)
  5. However, StaticSecret.value is defined as value: SecretStr (required, not optional), so validation fails

Error Message

pydantic_core._pydantic_core.ValidationError: 6 validation errors for ConversationState
secret_registry.secret_sources.LLM_API_KEY.value
  Field required [type=missing, input_value={"description": "This is ..."}, input_type=dict]
secret_registry.secret_sources.DD_API_KEY.value
  Field required [type=missing, input_value={"description": "Datadog API Key"}, input_type=dict]
...

Steps to Reproduce

  1. Create a ConversationState with secrets in secret_registry
  2. Let the state auto-persist (or manually call _save_base_state())
  3. Restart the server/process
  4. Try to restore the conversation using ConversationState.create() with the same persistence_dir
  5. Observe the Pydantic validation error

Expected Behavior

The conversation should be restored successfully, either by:

  • Making StaticSecret.value optional (value: SecretStr | None = None)
  • Or gracefully handling the case where secrets cannot be restored (with appropriate logging/warning)
  • Or always using a cipher when persisting secrets

Actual Behavior

The conversation fails to restore with a validation error, making the conversation permanently inaccessible.

Environment

  • openhands-sdk version: 1.7.4 (also verified in 1.8.1)
  • Discovered in production via Datadog logs

Affected Files

  • openhands/sdk/secret/secrets.py - StaticSecret class
  • openhands/sdk/utils/pydantic_secrets.py - serialize_secret() and validate_secret() functions
  • openhands/sdk/conversation/state.py - _save_base_state() method

Suggested Fix

Option 1: Make StaticSecret.value optional and handle None gracefully:

class StaticSecret(SecretSource):
    value: SecretStr | None = None

    def get_value(self):
        return self.value.get_secret_value() if self.value else None

Option 2: Skip serializing secrets that would be redacted, or exclude secret_registry from persistence entirely if no cipher is available.

Option 3: Always require a cipher when persisting conversation state with secrets.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions