Repository navigation
Description
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:
- When
ConversationState._save_base_state()is called, it usesmodel_dump_json(exclude_none=True)without providing a cipher orexpose_secretscontext - The
serialize_secret()function inutils/pydantic_secrets.pyreturns the rawSecretStrobject when no cipher/expose_secrets is provided (line 28) - Pydantic then serializes this as
"**********"(default masking) - When deserializing,
validate_secret()sees"**********"and returnsNone(line 52:if secret_value == "**********": return None) - However,
StaticSecret.valueis defined asvalue: 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
- Create a
ConversationStatewith secrets insecret_registry - Let the state auto-persist (or manually call
_save_base_state()) - Restart the server/process
- Try to restore the conversation using
ConversationState.create()with the same persistence_dir - Observe the Pydantic validation error
Expected Behavior
The conversation should be restored successfully, either by:
- Making
StaticSecret.valueoptional (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-StaticSecretclassopenhands/sdk/utils/pydantic_secrets.py-serialize_secret()andvalidate_secret()functionsopenhands/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 NoneOption 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.
Activity