Skip to content

Add configurable User-Agent (OPENAI_USER_AGENT) to config - #800

Open
guohai wants to merge 1 commit into
TheR1D:mainfrom
guohai:feature/configurable-user-agent
Open

guohai wants to merge 1 commit into
TheR1D:mainfrom
guohai:feature/configurable-user-agent

Conversation

@guohai

@guohai guohai commented Aug 28, 2026 •

Copy link
Copy Markdown

Motivation

Some API proxies and gateways filter requests by the User-Agent header. For example, a gateway may return 403 for any request whose UA matches OpenAI/Python* — the OpenAI SDK's default — while the identical request body succeeds via curl. In that case sgpt fails with:

PermissionDeniedError: Your request was blocked.

Since the OpenAI SDK provides no environment variable to override its User-Agent, sgpt users behind such gateways currently have no workaround short of patching site-packages.

Changes

  • New config option OPENAI_USER_AGENT in ~/.config/shell_gpt/.sgptrc (also usable as an environment variable, like other options).
  • Default value is "default", which keeps the OpenAI SDK's built-in User-Agent — behavior is unchanged for existing users.
  • Any other value is passed to the OpenAI client via default_headers={"User-Agent": ...} (follows the same pattern as API_BASE_URL).
  • Documented in the README's runtime configuration section.

Testing

  • pytest (29 passed), black --check, isort --check-only, mypy, and ruff all pass.
  • Verified end-to-end against a UA-filtering gateway: with OPENAI_USER_AGENT=default the request is rejected with 403; with a custom value the request succeeds.

Some API proxies and gateways filter requests based on the User-Agent
header (e.g. blocking the OpenAI SDK's default "OpenAI/Python ..."),
which makes sgpt fail with PermissionDeniedError even though the same
request succeeds via curl.

Add an OPENAI_USER_AGENT config option (also available as an
environment variable). When set to anything other than "default", it
is passed to the OpenAI client via default_headers. The default value
keeps the SDK's built-in User-Agent, so existing behavior is unchanged.
@guohai
guohai force-pushed the feature/configurable-user-agent branch from 7ea9594 to 401d7e5 Compare August 28, 2026 08:44

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.

1 participant