Repository navigation
Description
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
DiscriminatedUnionMixin.resolve_kind() looks the discriminator up with subclasses.get(kind) (utils/models.py#L365-L367).`` When a payload's kind is a list or an object, that raises `TypeError: unhashable type`, which isn't a `pydantic.ValidationError`. Every polymorphic model is affected: events, agents, confirmation policies, security analyzers and secret sources. The agent server answers 500 instead of 422 for such a request body, for example:
POST /api/conversations/{id}/confirmation_policywith{"policy": {"kind": []}}POST /api/conversationswith a secret{"S": {"kind": []}}
This is the third case of the same rule. Two fixes to this validator landed on 22 Oct: efec2c3 (#5798) refused non-object input with a ValidationError, and fc65d71 (#5770) stopped validation from mutating its input.
Expected Behavior
For any JSON value, model_validate either succeeds or raises pydantic.ValidationError, and the agent server answers 422.
Actual Behavior
TypeError: unhashable type: 'list' (or 'dict'), and HTTP 500.
Steps to Reproduce
Run the code sample below on main (28f4dfe).
Acceptance Criteria
- A non-string
kind(list, object, number, bool, null) raisesValidationErrorfrommodel_validateon any discriminated-union base. - The two agent-server requests above return 422.
SDK Version
main at 28f4dfe
Python Version
3.13
Operating System
Linux
Minimal Code Sample
from openhands.sdk.security.confirmation_policy import ConfirmationPolicyBase
ConfirmationPolicyBase.model_validate({"kind": []})
# TypeError: unhashable type: 'list'Evidence and Additional Context
Found by the property "for any JSON value, validation succeeds or raises ValidationError, never another exception type, and never mutates its input". The same property fails on the parents of efec2c3 and fc65d71. See #6486.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Verified against main (28f4dfe) with a local reproduction: for any discriminated-union base (ConfirmationPolicyBase, SecretSource, Event, AgentBase, SecurityAnalyzerBase), model_validate on {"kind": []} or {"kind": {}} raises TypeError: unhashable type from subclasses.get(kind) in resolve_kind() (openhands-sdk/openhands/sdk/utils/models.py), while hashable non-string kinds (3, True, None, unknown strings) already raise pydantic.ValidationError. The failure also propagates through plain pydantic parent fields of a union type and through TypeAdapter(dict[str, SecretSource]) - the path launch.py and RemoteState use - so the agent server answers 500 for such request bodies where the established contract is 422 (feature-map F08.policy-errors/F08.secrets-errors pin unknown-kind errors as 422, and #5798 added matching router tests). This is the third case of the same validator rule: #5798 handled non-object input, #5770 stopped input mutation; both landed in the shared _validate_subtype validator and tests/sdk/utils/test_discriminated_union.py.
Scope: route non-string kind values through the same ValueError -> ValidationError path the mixin already uses for unknown kinds, from the shared DiscriminatedUnionMixin validator (or resolve_kind) so all bases are covered at once, with regression tests in the existing test files.
Non-goals: no change to how valid string kinds are resolved or to the JSON-schema discriminator output; no new public API or error-message format beyond the existing "Unknown kind" path; no global agent-server error-handler overhaul (only these validation paths); no change to tool.py's separate Action/Observation kind validators, which already guard isinstance(v, str) before calling resolve_kind; none of the other live bugs from #6486 (#6487-#6494).
Acceptance Criteria
- For each discriminated-union base (
ConfirmationPolicyBase,SecretSource,Event,AgentBase,SecurityAnalyzerBase),model_validatewith any JSON value askind(including list, object, number, bool, null) either succeeds or raisespydantic.ValidationError- neverTypeErroror any other exception type. - The same property holds when the union field is nested inside a plain pydantic parent model and when validated through
TypeAdapter(dict[str, SecretSource])(the agent-server launch andRemoteStatepaths). - Valid payloads are unaffected:
{"kind": "NeverConfirm"}resolves toNeverConfirm,{"kind": "StaticSecret", "value": ...}resolves toStaticSecret, and existing discriminated-union round-trip tests pass unchanged. -
POST /api/conversations/{id}/confirmation_policywith{"policy": {"kind": []}}returns 422 (not 500) andset_confirmation_policyis not called. -
POST /api/conversationswith a secret{"S": {"kind": []}}returns 422 (not 500) andstart_conversationis not called. - Regression tests cover the non-string-kind cases in the existing suites (
tests/sdk/utils/test_discriminated_union.py,tests/agent_server/test_conversation_router.py) and the full existing suites in those files pass onmainwithout modification.
Activity