Skip to content

fix(service): correct network error classification in memory HTTP clients and make delete_thread cleanup robust - #64

Merged
Zhiyue-Wang merged 2 commits into
huaweicloud:mainfrom
Isuzu-ren:fix/service-exception-semantics
Oct 12, 2026
Merged

Zhiyue-Wang merged 2 commits into
huaweicloud:mainfrom
Isuzu-ren:fix/service-exception-semantics

Conversation

@Isuzu-ren

Copy link
Copy Markdown
Contributor

Description

Summary

This PR fixes two related issues in the SDK.

First, network errors in the memory HTTP clients were previously wrapped as MemoryAPIException with status_code=503, which falsely implies the server returned a "Service Unavailable" response. In reality these failures (timeouts, connection refused, DNS errors) never produced an HTTP response at all. They are now reported with status_code=0 to signal "no HTTP response", and the original exception is preserved through the standard exception chain so callers can inspect the root cause via __cause__. As a related cleanup, 2xx responses with a non-JSON body are no longer mislabeled as network errors; they now raise MemoryAPIException with a dedicated PARSE_ERROR error code while keeping the actual HTTP status code.

Second, MemoryAPIException was defined twice, once in each of the sync and async memory service modules, which made the two classes distinct despite sharing a name. It is now defined once in the shared HTTP client module and exported from the public service package, together with two new helper properties (is_network_error and retryable) that let callers classify errors without string matching on error codes.

Third, delete_thread and adelete_thread in the LangGraph checkpoint saver were restructured so that the writes session is deleted before the main session, and the in-memory persisted-count cleanup always runs via a finally block even when session deletion fails. The existing swallow-exception contract is preserved: deletion failures are logged but not propagated, and idempotent behavior is unchanged.

Behavior changes

  • Network errors: status_code changes from 503 to 0 (no server response).
  • New PARSE_ERROR error code for 2xx responses with invalid JSON bodies.
  • MemoryAPIException is now importable from the public service package (is_network_error / retryable properties added).
  • delete_thread / adelete_thread: writes session deleted first; persisted-count cleanup guaranteed via try/finally; swallow-exception contract unchanged.

Backward compatibility is maintained: MemoryAPIException remains a subclass of APIException, so existing except APIException handlers continue to work, and the package export is purely additive.

Verification

Verified with unit tests covering both synchronous and asynchronous client paths. The tests exercise the full error classification flow: timeouts and connection errors surface as network errors with no HTTP status and preserve the original exception for inspection; real HTTP 503 and 404 responses keep their actual status codes and are correctly distinguished from network-level failures; 2xx responses with non-JSON bodies are classified as parse errors rather than network errors; and successful 2xx responses pass through unchanged. The new classification properties were also asserted across network, rate-limit, server-error, and client-error cases to confirm retry semantics. For the checkpoint saver, tests verify that deleting a thread removes the writes session before the main session, that the persisted-count tracking is always cleaned up even when deletion fails, and that both sync and async paths behave identically without propagating deletion errors. A simulated end-to-end data flow exercise covering these same scenarios was run successfully to confirm the changes work together in practice.

Isuzu-ren and others added 2 commits September 4, 2026 15:01
…ents

Network errors (timeout, connection refused, etc.) were previously
wrapped as MemoryAPIException with status_code=503, falsely implying a
server response. They now use status_code=0 to signal "no HTTP
response", and the original exception is preserved via __cause__.

2xx responses with a non-JSON body are now classified as PARSE_ERROR
instead of falling into the network-error bucket.

MemoryAPIException was defined twice (sync and async modules); it is now
defined once in http_client.py and exported from the service package so
the new is_network_error / retryable helper properties are usable from
the public API surface.
delete_thread / adelete_thread now delete the writes session before the
main session, and _persisted_count cleanup runs in a finally block so it
always executes even when delete_session raises. The swallow-exception
contract is unchanged.

Adds a formal unit test suite for AgentArtsMemorySessionSaver
(put_writes / aput_writes, get_tuple / aget_tuple, list / alist,
delete_thread / adelete_thread, and context managers).
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@3a9ec7d). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #64   +/-   ##
=======================================
  Coverage        ?   61.66%           
=======================================
  Files           ?      108           
  Lines           ?     9566           
  Branches        ?     1495           
=======================================
  Hits            ?     5899           
  Misses          ?     3213           
  Partials        ?      454           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Zhiyue-Wang
Zhiyue-Wang merged commit db9a8e5 into huaweicloud:main Oct 12, 2026
6 checks passed
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.

3 participants