fix(api): close the HTTP transport after partition requests

AF
9h ago
+128-37

Summary

partition_via_api() creates an SDK client for each call, but does not enter the SDK's context manager. HTTP transport cleanup therefore depends on object finalization. In particular, a retained exception traceback keeps the helper's SDK reference alive after a failed request.

Use the SDK's synchronous context manager around file preparation, request construction, the request, and response decoding. The owned HTTP transport is closed before the helper returns or propagates an exception. Caller-provided upload streams remain open.

Validation

  • Six new regression cases fail on unchanged main and pass with this change. They inspect a real SDK-created HTTPX client after success, request failure, unexpected status, invalid JSON, a missing input file, and missing upload metadata. Requests are replaced with local responses; the tests make no API calls.
  • pytest test_unstructured/partition/test_api_resource_cleanup.py test_unstructured/partition/test_api.py -q: 26 passed, 6 skipped. The skipped tests use the live service.
  • Full-repository Ruff lint and format checks pass.
  • git diff --check passes.
  • The full make test suite, which requires additional document-processing extras and services, has not been run.

Validation follow-up

  • The formal repository-wide make check command passes in the shared validation worktree containing both #4840 and #4841. This checks their combined tree; it is not a claim that the two remote heads were separately tested.
  • The current CI workflow run is completed with action_required and has started zero jobs. It supplies no CI test result yet. The full make test and coverage gates remain unverified and await hosted CI approval.

Checklist

  • The PR title is a user-facing release-note entry with a conventional commit prefix.
  • Apply the bug category label; a maintainer may need to apply it.
  • No breaking public API change. Request parameters, retry behavior, and returned elements are unchanged.