Skip to content

fix(server): record invalid upstream responses as gateway errors - #658

Open
JianhaoYu (JianhaoYu-AI) wants to merge 1 commit into
microsoft:mainfrom
JianhaoYu-AI:fix/gateway-invalid-upstream-json
Open

JianhaoYu (JianhaoYu-AI) wants to merge 1 commit into
microsoft:mainfrom
JianhaoYu-AI:fix/gateway-invalid-upstream-json

Conversation

@JianhaoYu-AI

Copy link
Copy Markdown

Summary

Malformed upstream JSON and JSON values such as null or arrays currently raise before the gateway records a model-request event. Non-JSON HTTP 200 responses instead become empty successful responses.

This change returns a structured upstream-response error, maps invalid responses below HTTP 400 to 502, and preserves existing upstream HTTP error statuses. The existing event path records the error and retry count; the in-flight counter is released as before. Valid object responses retain their current behavior.

Fixes #655.

Validation

Windows, Python 3.12.14, pytest 9.1.1; local dependencies, not the frozen lockfile. No model calls or GPU use.

  • Eight new endpoint cases fail on the unchanged source.
  • python -m pytest -q tests/server/test_proxy_response.py tests/server/test_endpoints.py tests/test_client.py --tb=short: 51 passed.
  • python -m pytest -q tests --tb=short: 117 passed, 9 failed, 3 skipped. The unchanged upstream baseline, excluding the new test file, gives 109 passed, the same 9 failed, 3 skipped. The failures reference POSIX os.killpg, unavailable on Windows; skips concern optional VERL dependencies.
  • Ruff lint/format, diff whitespace checks and all applicable changed-file pre-commit hooks passed.

The new tests mock the upstream transport, call the public gateway endpoint, and inspect the returned error, stored event, retry count and in-flight counter. Existing tests cover valid completions and provider-error objects.

Scope and review status

This checks JSON decoding and the top-level object shape, not the entire provider schema. Linux CI, GPU training, frozen-lock validation and whole-project type checking were not run.

Duplicate-work checks found #587 (request-body validation) and #574 (triplet export), which address different paths. A refreshed open-PR search did not find this response-ingestion fix.

AI assistance was used for investigation, implementation and test execution. This is a draft; submitting-author review is still pending. No claim of completed human review is made.

@JianhaoYu-AI
JianhaoYu (JianhaoYu-AI) marked this pull request as ready for review October 26, 2026 11:57
Copilot AI balanced review requested due to automatic review settings October 26, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused change preserves valid-response behavior and cleanup, with regression coverage for the reported failures and no identified blocking issues.

No open issues 🎉

What changed in this PR

Fixes #655 by turning invalid upstream responses into structured gateway errors so failed model requests remain observable.

Changes:

  • Handles malformed JSON, non-object JSON, and non-JSON responses, mapping statuses below 400 to 502 while preserving upstream error statuses.
  • Uses the resulting status for both the recorded event and gateway response, leaving valid-object handling unchanged.
  • Adds eight endpoint regression cases covering errors, event recording, retry counts, and in-flight cleanup.
File Description
tests/​server/​test_proxy_response.py Adds endpoint coverage for invalid upstream responses and failure observability.
agentlightning/​server/​proxy.py Normalizes invalid responses into structured errors before event capture.

Review effort: Balanced

Copy link
Copy Markdown
Author

The Test workflow is waiting for approval for this fork, while license/cla is green. Could a maintainer approve the workflow when convenient? The local regression commands and results are in the PR description.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway loses model-request events for malformed or non-object upstream JSON

2 participants