Skip to content

[Bug]: A secret value that is not an object returns 500 "'str'/'int' object has no attribute 'pop'" #5791

Closed

Description

@enyst

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

A secret value that is not a JSON object makes the Agent Server answer 500 instead of a validation error:

  • POST /api/conversations with a plain-string secret in the create body ("secrets": {"NAME": "value"}) returns 500 {"exception": "'str' object has no attribute 'pop'"}. POST /api/conversations/{id}/secrets accepts the same shape.
  • POST /api/conversations/{id}/secrets with a number, null or a boolean as a value returns 500 with 'int', 'NoneType' or 'bool' object has no attribute 'pop'.

Any other invalid body is a 422.

Expected Behavior

A non-object secret value is refused with 422 like any other invalid request body (or, for the plain string on create, accepted the way POST .../secrets accepts it), never a 500. Nothing is stored.

Actual Behavior

500 with "exception": "'str' object has no attribute 'pop'" (create) and 'int' / 'NoneType' / 'bool' object has no attribute 'pop' (POST .../secrets).

Steps to Reproduce

  1. In a checkout of main: uv sync, then SESSION_API_KEY=dev-key uv run python -m openhands.agent_server --host 127.0.0.1 --port 8000.
  2. curl -s -H 'X-Session-API-Key: dev-key' -H 'Content-Type: application/json' http://127.0.0.1:8000/api/conversations -d '{"workspace": {"working_dir": "/tmp/w"}, "agent": {"kind": "Agent", "llm": {"model": "gpt-4o", "api_key": "k", "usage_id": "a"}, "tools": []}, "autotitle": false, "secrets": {"PLAIN": "value"}}' returns 500 ... 'str' object has no attribute 'pop'.
  3. With a conversation id $ID: curl -s -H 'X-Session-API-Key: dev-key' -H 'Content-Type: application/json' http://127.0.0.1:8000/api/conversations/$ID/secrets -d '{"secrets": {"N": 5}}' returns 500 ... 'int' object has no attribute 'pop' (same with null and true).

With the verify-agent-server skill: control-agent-server map run --file F04 --only F04.create-secrets-plain --fresh and control-agent-server map run --file F08 --only F08.secrets-bad-value --fresh report the known-bug bullets as expected failures while the bug lasts.

Acceptance Criteria

  • POST /api/conversations with a non-object secret value (string, number, null, boolean) answers 422 (or accepts a plain string), never 500, and starts no conversation.
  • POST /api/conversations/{id}/secrets with a number, null or boolean value answers 422; plain strings, {"value": ...} and StaticSecret objects are still accepted.
  • DiscriminatedUnionMixin.model_validate of a non-dict input raises pydantic.ValidationError, not AttributeError; unit tests cover it.
  • The map bullets F04.create-secrets-plain and F08.secrets-bad-value pass with their # bug markers removed.

Installation Method

uv sync in a checkout of main

Version

main at 6a298a4

Additional Context

Root cause: DiscriminatedUnionMixin._validate_subtype (openhands-sdk/openhands/sdk/utils/models.py:209) calls data.pop("kind", None) (lines 235 and 242) on the raw input without checking that it is a dict. For a string, number, null or boolean the AttributeError escapes pydantic validation (pydantic only converts ValueError/AssertionError), so FastAPI's generic handler returns 500.

  • Create: ConversationConfig.secrets: dict[str, SecretSource] (openhands-sdk/openhands/sdk/conversation/request.py:169) has no string coercion, so a plain string reaches the mixin.
  • POST .../secrets: UpdateSecretsRequest.convert_string_secrets (openhands-agent-server/openhands/agent_server/models.py:459) converts strings and {"value": ...} dicts but passes other values through unchanged.

Every DiscriminatedUnionMixin model (tools, events, confirmation policies, security analyzers...) shares the same crash on a non-object input. Related: #5770 changes the dict path of the same validator.

Found by the first full agentic run of verify-agent-server; tracked in #5653.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Confirmed on main. DiscriminatedUnionMixin._validate_subtype (openhands-sdk/openhands/sdk/utils/models.py) calls data.pop("kind", None) without checking that the input is a dict, so a non-object value raises AttributeError, which escapes Pydantic validation and surfaces as a 500. On the create path ConversationConfig.secrets: dict[str, SecretSource] has no pre-validator, and the agent-server's UpdateSecretsRequest.convert_string_secrets passes numbers, null and booleans through unchanged, so all of them reach the mixin. The verify-agent-server map bullets F04.create-secrets-plain and F08.secrets-bad-value already mark this as a known bug and expect 422, and F08.secrets-errors already states that a non-object secrets payload is 422, so 422 is the repository's established contract for both endpoints.

Bounded scope: make a non-object secret value fail request validation with 422 (never 500, nothing stored) on both POST /api/conversations and POST /api/conversations/{id}/secrets, and make DiscriminatedUnionMixin.model_validate of a non-dict input raise pydantic.ValidationError instead of AttributeError, which covers every DiscriminatedUnionMixin model (tools, events, confirmation policies, security analyzers). Because the repository's contract for a non-object secret is rejection, this triage selects 422 for the create endpoint rather than adding plain-string coercion to ConversationConfig.secrets.

Explicit non-goals: no change to the accepted shapes on POST /api/conversations/{id}/secrets (plain strings, {"value": ...}, and StaticSecret objects stay accepted); no new string coercion on the create endpoint; no change to the dict-path input-mutation behavior handled by open PR #5770 (#5652); no broader REST error-handling refactor.

Priority: low. Found by the first automated verify-agent-server run (#5653); no user report, production impact, or customer evidence is recorded.

Acceptance Criteria

  • POST /api/conversations with a secrets value that is a string, number, null, or boolean answers 422 (never 500), the response is a standard validation-error body that does not contain an exception field, and no conversation is created.
  • POST /api/conversations/{id}/secrets with a number, null, or boolean value answers 422 (never 500), and nothing is stored: a GET /api/conversations/{id} afterwards does not list the rejected name, and any secret set earlier is still present.
  • On POST /api/conversations/{id}/secrets, plain strings, {"value": ...} dicts, and StaticSecret objects are still accepted with 200, and the stored secret is usable by a later command.
  • DiscriminatedUnionMixin.model_validate of a non-dict input (str, int, None, bool) raises pydantic.ValidationError, not AttributeError, for both an abstract base and a concrete subclass; a unit test in tests/sdk/utils/test_discriminated_union.py covers it.
  • POST /api/conversations with a well-formed StaticSecret still returns 201, confirming the accepted create path is unchanged.
  • The # bug markers are removed from F04.create-secrets-plain and F08.secrets-bad-value, and control-agent-server map run --file F04 --only F04.create-secrets-plain --fresh and control-agent-server map run --file F08 --only F08.secrets-bad-value --fresh pass.

Activity

  1. added
    bug
    ready-for-dev
    on Oct 13, 2026
  2. enyst commented on Oct 13, 2026

    @enyst
    MemberAuthor

    I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

    Fix proposed in #5798.

    Decision needed (maintainer): should the create body accept a plain-string secret like POST .../secrets does, or refuse it?

    • Implemented: 422. It is the smallest fix of the single root cause (DiscriminatedUnionMixin now hands a non-dict input to pydantic, which reports a model_type ValidationError), and the create body's schema (dict[str, SecretSource]) has never accepted plain strings.
    • Alternative: add the convert_string_secrets coercion to ConversationConfig.secrets too; plain strings on create would then be 201, the OpenAPI schema for secrets would widen, and the F04.create-secrets-plain map bullet would expect 201.

    Numbers, null and booleans on POST .../secrets are 422 either way.


    Generated by Claude Code

  3. enyst commented on Oct 13, 2026

    @enyst
    MemberAuthor

    I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

    The triage's choice (422 on create, no new coercion) is what #5798 implements. On its acceptance criteria:

    • 422 bodies are FastAPI's standard detail list (model_attributes_type at body.secrets.<NAME>), with no exception field; the router tests assert the location, and the map recipes now check the error type.
    • Nothing is stored or started: the tests assert start_conversation / update_secrets are not called. Accepted shapes on POST .../secrets and a well-formed StaticSecret on create are unchanged (existing tests and F04.create-secrets, F08.secrets-update).
    • The mixin raises ValidationError for str, int, None, bool and a list on an abstract base, concrete subclasses and a nested field (tests/sdk/utils/test_discriminated_union.py).
    • Map markers removed; both bullets pass, and on main each was xfail twice.

    A maintainer can still choose parity with POST .../secrets (accept plain strings on create); that would add coercion to ConversationConfig.secrets and flip F04.create-secrets-plain to 201.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions