Skip to content

fix(orgs): return 422 with the reason when org default settings fail validation - #1537

Draft
neubig wants to merge 2 commits into
mainfrom
fix/1530-org-settings-validation-error
Draft

neubig wants to merge 2 commits into
mainfrom
fix/1530-org-settings-validation-error

Conversation

@neubig

@neubig neubig commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

This PR was created by an AI agent (OpenHands) on behalf of @neubig.

HUMAN:

  • A human has tested these changes.

AGENT:


Why

OrgService.update_org_with_permissions wrapped every exception from OrgStore.update_org in OrgDatabaseError. That included SDK validation errors, such as LLMContextWindowTooSmallError for a model whose context window is below 16,384 tokens, or a pydantic ValidationError for a bad field type. PATCH /api/organizations/{org_id}/settings then answered 500 "Failed to update organization defaults settings". The admin could not tell what was wrong or how to fix it. PATCH /api/organizations/{org_id} had the same problem.

Summary

  • OrgStore._merge_and_validate_settings now turns LLMContextWindowTooSmallError and pydantic ValidationError into a new OrgSettingsValidationError. Its detail has a message, the field, and, for the context window, context_window and min_required.
  • OrgService.update_org_with_permissions lets OrgSettingsValidationError through instead of wrapping it in OrgDatabaseError.
  • Both org settings routes map it to 422 with that detail. The frontend already shows detail.message in the save-error toast (retrieveAxiosErrorMessage).
  • OrgSettingsValidationError is a ValueError, so POST /api/organizations/app (which already maps ValueError to 400) now also returns the reason for a too-small context window instead of 500.
  • New regression tests in tests/unit/server/routes/test_org_settings_validation.py. They call the real routes, OrgService, OrgStore and SDK validation on the PostgreSQL test database. Only authentication and the LiteLLM key-rotation dependency are overridden.

Issue Number

Fixes #1530

How to Test

uv run pytest tests/unit/server/routes/test_org_settings_validation.py -q
# main:   3 failed (500 instead of 422)
# branch: 3 passed

uv run pytest -n 4 tests/unit/test_org_service.py tests/unit/test_org_store.py \
  tests/unit/server/routes/test_orgs.py tests/unit/server/routes/test_org_defaults_settings.py \
  tests/unit/server/routes/test_org_settings_validation.py \
  tests/unit/storage/test_org_app_settings_store.py tests/unit/server/routes/test_org_models.py -q
# 337 passed

The screenshots also run the repro test from the issue (not committed), which prints each response.

Video/Screenshots

Before: with the four source files from main, a too-small context window and a bad field type both return 500 with a generic message. All five tests fail.

Before: 500 Failed to update organization defaults settings

After: on this branch the same requests return 422. The body names the field and the 16,384-token minimum. Nothing is saved, and a supported value still saves with 200.

After: 422 with message, field, context_window and min_required

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • The .pr/1530/ screenshots are temporary PR artifacts. They are removed on approval.
  • For a pydantic error, field is the pydantic location under the settings diff. For agent settings it includes the union tag, for example agent_settings_diff.openhands.llm.temperature.
  • No UI screenshot: the org LLM settings page saves through the org LLM profile endpoints (OrgProfilesService), not this route, so a UI capture would not exercise this code path. The screenshots show the backend tests instead.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-5a4ff94

…validation

OrgService.update_org_with_permissions wrapped every exception from
OrgStore.update_org in OrgDatabaseError, so SDK validation failures
(LLMContextWindowTooSmallError, pydantic ValidationError) reached the
admin as a generic 500. OrgStore._merge_and_validate_settings now raises
OrgSettingsValidationError with the message, field and limits; the
service lets it through and both org settings routes answer 422.

Fixes #1530

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR contains a .pr/ directory with PR-specific documents. This directory will be automatically removed when the PR is approved.

For fork PRs: Manual removal is required before merging.

@github-actions github-actions Bot added the type: fix A bug fix label Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  server/routes
  org_models.py 75-76
  orgs.py 451, 955
  storage
  org_service.py 609
  org_store.py 602-618
Project Total  

This report was generated by python-coverage-comment-action

This branch has not been deployed

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

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Org default settings save returns a generic 500 when the LLM context window is too small (validation errors wrapped as OrgDatabaseError)

2 participants