openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providers

Open Kuldeeep18 opened this issue 2 months ago • 4 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [x] This is an issue with the Python library

Describe the bug

In src/openai/auth/_workload.py, the built-in workload identity subject token providers (azure_managed_identity_token_provider(), gcp_id_token_provider(), and k8s_service_account_token_provider()) catch their own intentionally raised SubjectTokenProviderError exceptions inside generic except Exception as e: blocks.

This leads to two issues:

  1. Lost response property: SubjectTokenProviderError defines a response: httpx2.Response | None attribute. The provider functions explicitly pass response=response when the metadata server returns an error status (response.is_error) or an empty body. However, the outer except Exception as e: intercepts this error (as SubjectTokenProviderError inherits from OpenAIError -> Exception) and instantiates a brand new SubjectTokenProviderError(...) without passing response, setting err.response to None. Callers attempting to inspect err.response for headers, status codes, or error details receive None.
  2. Duplicated error message prefixes: The re-wrapped exception message duplicates the prefix (e.g. "Failed to fetch Azure subject token from IMDS: Failed to fetch Azure subject token from IMDS: HTTP 500").
  3. Redundant exception nesting: A SubjectTokenProviderError is chained as the __cause__ of an identical SubjectTokenProviderError.

To Reproduce

Steps:

  1. Initialize azure_managed_identity_token_provider() or gcp_id_token_provider() with an http_client mock returning an HTTP 500 (or non-200) error.
  2. Call provider["get_token"]().
  3. Catch SubjectTokenProviderError and inspect err.response and str(err).

Code snippets

import httpx2
from openai.auth._workload import azure_managed_identity_token_provider
from openai._exceptions import SubjectTokenProviderError

mock_client = httpx2.Client(
    transport=httpx2.MockTransport(lambda req: httpx2.Response(500, text="Internal Server Error"))
)
provider = azure_managed_identity_token_provider(http_client=mock_client)

try:
    provider["get_token"]()
except SubjectTokenProviderError as err:
    print(f"err.response is: {err.response}")  # Actual: None | Expected: <Response [500]>
    print(f"Error message: {err}")
    print(f"err.__cause__: {repr(err.__cause__)}")

Actual Output:

err.response is: None
Error message: Failed to fetch Azure subject token from IMDS: Failed to fetch Azure subject token from IMDS: HTTP 500
err.__cause__: SubjectTokenProviderError('Failed to fetch Azure subject token from IMDS: HTTP 500')

Expected Output: err.response should retain the <Response [500 Internal Server Error]> object attached when the initial SubjectTokenProviderError was raised, and the error message should not contain duplicated prefixes.

Proposed fix

In src/openai/auth/_workload.py: In k8s_service_account_token_provider, azure_managed_identity_token_provider, and gcp_id_token_provider, re-raise SubjectTokenProviderError before the generic except Exception block:

        except SubjectTokenProviderError:
            raise
        except Exception as e:
            raise SubjectTokenProviderError(f"...: {e}") from e

OS

All platforms (cross-platform library error handling)

Python version

Python 3.10+

Library version

openai v1.x / latest main

Kuldeeep18 avatar Oct 02 '26 07:10 Kuldeeep18

I'd like to work on this.

The fix is straightforward: in src/openai/auth/_workload.py, each of the three workload identity token providers (k8s_service_account_token_provider, azure_managed_identity_token_provider, gcp_id_token_provider) has an except Exception as e: block that catches SubjectTokenProviderError and re-wraps it without the response property.

The fix is to add except SubjectTokenProviderError: raise before each except Exception block, so the original exception with its response property is preserved.

I'll add regression tests that verify:

  1. err.response is not None when the metadata server returns an error
  2. The error message does not have duplicated prefixes
  3. err.__cause__ is not a redundant SubjectTokenProviderError

DawnofGenX avatar Oct 02 '26 12:10 DawnofGenX

tested and pushed fix + unit tests for all 3 providers: branch: https://github.com/mohmedmm/openai-python/tree/fix-workload-identity-provider-response commit: https://github.com/mohmedmm/openai-python/commit/3e575fe

repo restricts direct external PR creation to collaborators, but maintainers can pull or cherry-pick directly from mohmedmm:fix-workload-identity-provider-response.

mohmedmm avatar Oct 02 '26 12:10 mohmedmm

Thanks @mohmedmm — you've got a fix and tests up, so I'll leave it to you. Note this repo only accepts PRs from collaborators, so you may hit a permission wall on gh pr create; if so, the maintainers will need to pull it in. Not pursuing this from my side.

DawnofGenX avatar Oct 07 '26 16:10 DawnofGenX

I've implemented and verified a fix for this issue on prasad2512/openai-python@af204a1.

Proposed Fix:

In src/openai/auth/_workload.py: Re-raise SubjectTokenProviderError before the outer except Exception as e: catch in k8s_service_account_token_provider(), azure_managed_identity_token_provider(), and gcp_id_token_provider():

        except SubjectTokenProviderError:
            raise
        except Exception as e:
            raise SubjectTokenProviderError(f"...: {e}") from e

This preserves the attached err.response object, avoids duplicate prefix wrapping, and prevents redundant nested exception chaining. Unit tests for each error branch were also added in tests/test_auth.py. Since PRs on this repo are restricted to collaborators, maintainers can cherry-pick this commit.

prasad2512 avatar Oct 19 '26 16:10 prasad2512

Branch and regression tests covering all three providers have been verified and ready on mohmedmm:fix-workload-identity-provider-response (commit 3e575fe) since Oct 2, as noted above. Available for cherry-pick whenever maintainers are ready.

mohmedmm avatar Oct 19 '26 21:10 mohmedmm