Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Add ChatGPT subscription support and responses bridge (#19030)

Manuel Schweigert29adf3431371
diff --git a/docs/my-website/docs/providers/chatgpt.md b/docs/my-website/docs/providers/chatgpt.md
new file mode 100644
index 00000000000..156bbf99df6
--- /dev/null
+++ b/docs/my-website/docs/providers/chatgpt.md
@@ -0,0 +1,84 @@
+# ChatGPT Subscription
+
+Use ChatGPT Pro/Max subscription models through LiteLLM with OAuth device flow authentication.
+
+| Property | Details |
+|-------|-------|
+| Description | ChatGPT subscription access (Codex + GPT-5.2 family) via ChatGPT backend API |
+| Provider Route on LiteLLM | `chatgpt/` |
+| Supported Endpoints | `/responses`, `/chat/completions` (bridged to Responses for supported models) |
+| API Reference | https://chatgpt.com |
+
+ChatGPT subscription access is native to the Responses API. Chat Completions requests are bridged to Responses for supported models (for example `chatgpt/gpt-5.2`).
+
+Notes:
+- The ChatGPT subscription backend rejects token limit fields (`max_tokens`, `max_output_tokens`, `max_completion_tokens`) and `metadata`. LiteLLM strips these fields for this provider.
+- `/v1/chat/completions` honors `stream`. When `stream` is false (default), LiteLLM aggregates the Responses stream into a single JSON response.
+
+## Authentication
+
+ChatGPT subscription access uses an OAuth device code flow:
+
+1. LiteLLM prints a device code and verification URL
+2. Open the URL, sign in, and enter the code
+3. Tokens are stored locally for reuse
+
+## Usage - LiteLLM Python SDK
+
+### Responses (recommended for Codex models)
+
+```python showLineNumbers title="ChatGPT Responses"
+import litellm
+
+response = litellm.responses(
+ model="chatgpt/gpt-5.2-codex",
+ input="Write a Python hello world"
+)
+
+print(response)
+```
+
+### Chat Completions (bridged to Responses)
+
+```python showLineNumbers title="ChatGPT Chat Completions"
+import litellm
+
+response = litellm.completion(
+ model="chatgpt/gpt-5.2",
+ messages=[{"role": "user", "content": "Write a Python hello world"}]
+)
+
+print(response)
+```
+
+## Usage - LiteLLM Proxy
+
+```yaml showLineNumbers title="config.yaml"
+model_list:
+ - model_name: chatgpt/gpt-5.2
+ model_info:
+ mode: responses
+ litellm_params:
+ model: chatgpt/gpt-5.2
+ - model_name: chatgpt/gpt-5.2-codex
+ model_info:
+ mode: responses
+ litellm_params:
+ model: chatgpt/gpt-5.2-codex
+```
+
+```bash showLineNumbers title="Start LiteLLM Proxy"
+litellm --config config.yaml
+```
+
+## Configuration
+
+### Environment Variables
+
+- `CHATGPT_TOKEN_DIR`: Custom token storage directory
+- `CHATGPT_AUTH_FILE`: Auth file name (default: `auth.json`)
+- `CHATGPT_API_BASE`: Override API base (default: `https://chatgpt.com/backend-api/codex`)
+- `OPENAI_CHATGPT_API_BASE`: Alias for `CHATGPT_API_BASE`
+- `CHATGPT_ORIGINATOR`: Override the `originator` header value
+- `CHATGPT_USER_AGENT`: Override the `User-Agent` header value
+- `CHATGPT_USER_AGENT_SUFFIX`: Optional suffix appended to the `User-Agent` header
diff --git a/docs/my-website/sidebars.js b/docs/my-website/sidebars.js
index 102e3dfe1c5..d1abe45332f 100644
--- a/docs/my-website/sidebars.js
+++ b/docs/my-website/sidebars.js
@@ -717,6 +717,7 @@ const sidebars = {
"providers/galadriel",
"providers/github",
"providers/github_copilot",
+ "providers/chatgpt",
"providers/gradient_ai",
"providers/groq",
"providers/helicone",
diff --git a/litellm/__init__.py b/litellm/__init__.py
index 9eb3f075d5e..83cbcfda3f1 100644
--- a/litellm/__init__.py
+++ b/litellm/__init__.py
@@ -557,6 +557,7 @@ docker_model_runner_models: Set = set()
amazon_nova_models: Set = set()
stability_models: Set = set()
github_copilot_models: Set = set()
+chatgpt_models: Set = set()
minimax_models: Set = set()
aws_polly_models: Set = set()
gigachat_models: Set = set()
@@ -812,6 +813,8 @@ def add_known_models():
stability_models.add(key)
elif value.get("litellm_provider") == "github_copilot":
github_copilot_models.add(key)
+ elif value.get("litellm_provider") == "chatgpt":
+ chatgpt_models.add(key)
elif value.get("litellm_provider") == "minimax":
minimax_models.add(key)
elif value.get("litellm_provider") == "aws_polly":
@@ -1025,6 +1028,7 @@ models_by_provider: dict = {
"amazon_nova": amazon_nova_models,
"stability": stability_models,
"github_copilot": github_copilot_models,
+ "chatgpt": chatgpt_models,
"minimax": minimax_models,
"aws_polly": aws_polly_models,
"gigachat": gigachat_models,
@@ -1458,6 +1462,8 @@ if TYPE_CHECKING:
from .llms.github_copilot.chat.transformation import GithubCopilotConfig as GithubCopilotConfig
from .llms.github_copilot.responses.transformation import GithubCopilotResponsesAPIConfig as GithubCopilotResponsesAPIConfig
from .llms.github_copilot.embedding.transformation import GithubCopilotEmbeddingConfig as GithubCopilotEmbeddingConfig
+ from .llms.chatgpt.chat.transformation import ChatGPTConfig as ChatGPTConfig
+ from .llms.chatgpt.responses.transformation import ChatGPTResponsesAPIConfig as ChatGPTResponsesAPIConfig
from .llms.gigachat.chat.transformation import GigaChatConfig as GigaChatConfig
from .llms.gigachat.embedding.transformation import GigaChatEmbeddingConfig as GigaChatEmbeddingConfig
from .llms.nebius.chat.transformation import NebiusConfig as NebiusConfig
diff --git a/litellm/_lazy_imports_registry.py b/litellm/_lazy_imports_registry.py
index f37c4dc6d04..46abde4eb5e 100644
--- a/litellm/_lazy_imports_registry.py
+++ b/litellm/_lazy_imports_registry.py
@@ -253,6 +253,8 @@ LLM_CONFIG_NAMES = (
"IBMWatsonXAudioTranscriptionConfig",
"GithubCopilotConfig",
"GithubCopilotResponsesAPIConfig",
+ "ChatGPTConfig",
+ "ChatGPTResponsesAPIConfig",
"ManusResponsesAPIConfig",
"GithubCopilotEmbeddingConfig",
"NebiusConfig",
@@ -648,6 +650,8 @@ _LLM_CONFIGS_IMPORT_MAP = {
"GithubCopilotConfig": (".llms.github_copilot.chat.transformation", "GithubCopilotConfig"),
"GithubCopilotResponsesAPIConfig": (".llms.github_copilot.responses.transformation", "GithubCopilotResponsesAPIConfig"),
"GithubCopilotEmbeddingConfig": (".llms.github_copilot.embedding.transformation", "GithubCopilotEmbeddingConfig"),
+ "ChatGPTConfig": (".llms.chatgpt.chat.transformation", "ChatGPTConfig"),
+ "ChatGPTResponsesAPIConfig": (".llms.chatgpt.responses.transformation", "ChatGPTResponsesAPIConfig"),
"NebiusConfig": (".llms.nebius.chat.transformation", "NebiusConfig"),
"WandbConfig": (".llms.wandb.chat.transformation", "WandbConfig"),
"GigaChatConfig": (".llms.gigachat.chat.transformation", "GigaChatConfig"),
diff --git a/litellm/completion_extras/litellm_responses_transformation/handler.py b/litellm/completion_extras/litellm_responses_transformation/handler.py
index 6ec49ce0620..5c051797e8b 100644
--- a/litellm/completion_extras/litellm_responses_transformation/handler.py
+++ b/litellm/completion_extras/litellm_responses_transformation/handler.py
@@ -2,10 +2,12 @@
Handler for transforming /chat/completions api requests to litellm.responses requests
"""
-from typing import TYPE_CHECKING, Any, Coroutine, Union
+from typing import TYPE_CHECKING, Any, Coroutine, Optional, Union
from typing_extensions import TypedDict
+from litellm.types.llms.openai import ResponsesAPIResponse
+
if TYPE_CHECKING:
from litellm import CustomStreamWrapper, LiteLLMLoggingObj, ModelResponse
@@ -28,6 +30,71 @@ class ResponsesToCompletionBridgeHandler:
super().__init__()
self.transformation_handler = LiteLLMResponsesTransformationHandler()
+ @staticmethod
+ def _resolve_stream_flag(optional_params: dict, litellm_params: dict) -> bool:
+ stream = optional_params.get("stream")
+ if stream is None:
+ stream = litellm_params.get("stream", False)
+ return bool(stream)
+
+ @staticmethod
+ def _coerce_response_object(
+ response_obj: Any,
+ hidden_params: Optional[dict],
+ ) -> "ResponsesAPIResponse":
+ if isinstance(response_obj, ResponsesAPIResponse):
+ response = response_obj
+ elif isinstance(response_obj, dict):
+ try:
+ response = ResponsesAPIResponse(**response_obj)
+ except Exception:
+ response = ResponsesAPIResponse.model_construct(**response_obj)
+ else:
+ raise ValueError("Unexpected responses stream payload")
+
+ if hidden_params:
+ existing = getattr(response, "_hidden_params", None)
+ if not isinstance(existing, dict) or not existing:
+ setattr(response, "_hidden_params", dict(hidden_params))
+ else:
+ for key, value in hidden_params.items():
+ existing.setdefault(key, value)
+ return response
+
+ def _collect_response_from_stream(
+ self, stream_iter: Any
+ ) -> "ResponsesAPIResponse":
+ for _ in stream_iter:
+ pass
+
+ completed = getattr(stream_iter, "completed_response", None)
+ response_obj = getattr(completed, "response", None) if completed else None
+ if response_obj is None:
+ raise ValueError("Stream ended without a completed response")
+
+ hidden_params = getattr(stream_iter, "_hidden_params", None)
+ response = self._coerce_response_object(response_obj, hidden_params)
+ if not isinstance(response, ResponsesAPIResponse):
+ raise ValueError("Stream completed response is invalid")
+ return response
+
+ async def _collect_response_from_stream_async(
+ self, stream_iter: Any
+ ) -> "ResponsesAPIResponse":
+ async for _ in stream_iter:
+ pass
+
+ completed = getattr(stream_iter, "completed_response", None)
+ response_obj = getattr(completed, "response", None) if completed else None
+ if response_obj is None:
+ raise ValueError("Stream ended without a completed response")
+
+ hidden_params = getattr(stream_iter, "_hidden_params", None)
+ response = self._coerce_response_object(response_obj, hidden_params)
+ if not isinstance(response, ResponsesAPIResponse):
+ raise ValueError("Stream completed response is invalid")
+ return response
+
def validate_input_kwargs(
self, kwargs: dict
) -> ResponsesToCompletionBridgeHandlerInputKwargs:
@@ -87,7 +154,6 @@ class ResponsesToCompletionBridgeHandler:
from litellm import responses
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
- from litellm.types.llms.openai import ResponsesAPIResponse
validated_kwargs = self.validate_input_kwargs(kwargs)
model = validated_kwargs["model"]
@@ -113,6 +179,7 @@ class ResponsesToCompletionBridgeHandler:
**request_data,
)
+ stream = self._resolve_stream_flag(optional_params, litellm_params)
if isinstance(result, ResponsesAPIResponse):
return self.transformation_handler.transform_response(
model=model,
@@ -127,6 +194,21 @@ class ResponsesToCompletionBridgeHandler:
api_key=kwargs.get("api_key"),
json_mode=kwargs.get("json_mode"),
)
+ elif not stream:
+ responses_api_response = self._collect_response_from_stream(result)
+ return self.transformation_handler.transform_response(
+ model=model,
+ raw_response=responses_api_response,
+ model_response=model_response,
+ logging_obj=logging_obj,
+ request_data=request_data,
+ messages=messages,
+ optional_params=optional_params,
+ litellm_params=litellm_params,
+ encoding=kwargs.get("encoding"),
+ api_key=kwargs.get("api_key"),
+ json_mode=kwargs.get("json_mode"),
+ )
else:
completion_stream = self.transformation_handler.get_model_response_iterator(
streaming_response=result, # type: ignore
@@ -146,7 +228,6 @@ class ResponsesToCompletionBridgeHandler:
) -> Union["ModelResponse", "CustomStreamWrapper"]:
from litellm import aresponses
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
- from litellm.types.llms.openai import ResponsesAPIResponse
validated_kwargs = self.validate_input_kwargs(kwargs)
model = validated_kwargs["model"]
@@ -175,6 +256,7 @@ class ResponsesToCompletionBridgeHandler:
aresponses=True,
)
+ stream = self._resolve_stream_flag(optional_params, litellm_params)
if isinstance(result, ResponsesAPIResponse):
return self.transformation_handler.transform_response(
model=model,
@@ -189,6 +271,23 @@ class ResponsesToCompletionBridgeHandler:
api_key=kwargs.get("api_key"),
json_mode=kwargs.get("json_mode"),
)
+ elif not stream:
+ responses_api_response = await self._collect_response_from_stream_async(
+ result
+ )
+ return self.transformation_handler.transform_response(
+ model=model,
+ raw_response=responses_api_response,
+ model_response=model_response,
+ logging_obj=logging_obj,
+ request_data=request_data,
+ messages=messages,
+ optional_params=optional_params,
+ litellm_params=litellm_params,
+ encoding=kwargs.get("encoding"),
+ api_key=kwargs.get("api_key"),
+ json_mode=kwargs.get("json_mode"),
+ )
else:
completion_stream = self.transformation_handler.get_model_response_iterator(
streaming_response=result, # type: ignore
diff --git a/litellm/constants.py b/litellm/constants.py
index 3bdd943481e..f21e7517482 100644
--- a/litellm/constants.py
+++ b/litellm/constants.py
@@ -415,6 +415,7 @@ LITELLM_CHAT_PROVIDERS = [
"galadriel",
"gradient_ai",
"github_copilot", # GitHub Copilot Chat API
+ "chatgpt", # ChatGPT subscription API
"novita",
"meta_llama",
"featherless_ai",
@@ -613,6 +614,7 @@ openai_compatible_providers: List = [
"lm_studio",
"galadriel",
"github_copilot", # GitHub Copilot Chat API
+ "chatgpt", # ChatGPT subscription API
"novita",
"meta_llama",
"publicai", # PublicAI - JSON-configured provider
diff --git a/litellm/litellm_core_utils/get_llm_provider_logic.py b/litellm/litellm_core_utils/get_llm_provider_logic.py
index 21d69177336..807b66faec1 100644
--- a/litellm/litellm_core_utils/get_llm_provider_logic.py
+++ b/litellm/litellm_core_utils/get_llm_provider_logic.py
@@ -768,6 +768,14 @@ def _get_openai_compatible_provider_info( # noqa: PLR0915
) = litellm.GithubCopilotConfig()._get_openai_compatible_provider_info(
model, api_base, api_key, custom_llm_provider
)
+ elif custom_llm_provider == "chatgpt":
+ (
+ api_base,
+ dynamic_api_key,
+ custom_llm_provider,
+ ) = litellm.ChatGPTConfig()._get_openai_compatible_provider_info(
+ model, api_base, api_key, custom_llm_provider
+ )
elif custom_llm_provider == "novita":
api_base = (
api_base
diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py
new file mode 100644
index 00000000000..ff053730c35
--- /dev/null
+++ b/litellm/llms/chatgpt/authenticator.py
@@ -0,0 +1,388 @@
+import base64
+import json
+import os
+import time
+from typing import Any, Dict, Optional
+
+import httpx
+
+from litellm._logging import verbose_logger
+from litellm.llms.custom_httpx.http_handler import _get_httpx_client
+
+from .common_utils import (
+ CHATGPT_API_BASE,
+ CHATGPT_AUTH_BASE,
+ CHATGPT_CLIENT_ID,
+ CHATGPT_DEVICE_CODE_URL,
+ CHATGPT_DEVICE_TOKEN_URL,
+ CHATGPT_DEVICE_VERIFY_URL,
+ CHATGPT_OAUTH_TOKEN_URL,
+ GetAccessTokenError,
+ GetDeviceCodeError,
+ RefreshAccessTokenError,
+)
+
+TOKEN_EXPIRY_SKEW_SECONDS = 60
+DEVICE_CODE_TIMEOUT_SECONDS = 15 * 60
+DEVICE_CODE_COOLDOWN_SECONDS = 5 * 60
+DEVICE_CODE_POLL_SLEEP_SECONDS = 5
+
+
+class Authenticator:
+ def __init__(self) -> None:
+ self.token_dir = os.getenv(
+ "CHATGPT_TOKEN_DIR",
+ os.path.expanduser("~/.config/litellm/chatgpt"),
+ )
+ self.auth_file = os.path.join(
+ self.token_dir, os.getenv("CHATGPT_AUTH_FILE", "auth.json")
+ )
+ self._ensure_token_dir()
+
+ def get_api_base(self) -> str:
+ return (
+ os.getenv("CHATGPT_API_BASE")
+ or os.getenv("OPENAI_CHATGPT_API_BASE")
+ or CHATGPT_API_BASE
+ )
+
+ def get_access_token(self) -> str:
+ auth_data = self._read_auth_file()
+ if auth_data:
+ access_token = auth_data.get("access_token")
+ if access_token and not self._is_token_expired(auth_data, access_token):
+ return access_token
+ refresh_token = auth_data.get("refresh_token")
+ if refresh_token:
+ try:
+ refreshed = self._refresh_tokens(refresh_token)
+ return refreshed["access_token"]
+ except RefreshAccessTokenError as exc:
+ verbose_logger.warning(
+ "ChatGPT refresh token failed, re-login required: %s", exc
+ )
+
+ cooldown_remaining = self._get_device_code_cooldown_remaining(auth_data)
+ if cooldown_remaining > 0:
+ token = self._wait_for_access_token(cooldown_remaining)
+ if token:
+ return token
+
+ tokens = self._login_device_code()
+ return tokens["access_token"]
+
+ def get_account_id(self) -> Optional[str]:
+ auth_data = self._read_auth_file()
+ if not auth_data:
+ return None
+ account_id = auth_data.get("account_id")
+ if account_id:
+ return account_id
+ id_token = auth_data.get("id_token")
+ access_token = auth_data.get("access_token")
+ derived = self._extract_account_id(id_token or access_token)
+ if derived:
+ auth_data["account_id"] = derived
+ self._write_auth_file(auth_data)
+ return derived
+
+ def _ensure_token_dir(self) -> None:
+ if not os.path.exists(self.token_dir):
+ os.makedirs(self.token_dir, exist_ok=True)
+
+ def _read_auth_file(self) -> Optional[Dict[str, Any]]:
+ try:
+ with open(self.auth_file, "r") as f:
+ return json.load(f)
+ except IOError:
+ return None
+ except json.JSONDecodeError as exc:
+ verbose_logger.warning("Invalid ChatGPT auth file: %s", exc)
+ return None
+
+ def _write_auth_file(self, data: Dict[str, Any]) -> None:
+ try:
+ with open(self.auth_file, "w") as f:
+ json.dump(data, f)
+ except IOError as exc:
+ verbose_logger.error("Failed to write ChatGPT auth file: %s", exc)
+
+ def _is_token_expired(self, auth_data: Dict[str, Any], access_token: str) -> bool:
+ expires_at = auth_data.get("expires_at")
+ if expires_at is None:
+ expires_at = self._get_expires_at(access_token)
+ if expires_at:
+ auth_data["expires_at"] = expires_at
+ self._write_auth_file(auth_data)
+ if expires_at is None:
+ return True
+ return time.time() >= float(expires_at) - TOKEN_EXPIRY_SKEW_SECONDS
+
+ def _get_expires_at(self, token: str) -> Optional[int]:
+ claims = self._decode_jwt_claims(token)
+ exp = claims.get("exp")
+ if isinstance(exp, (int, float)):
+ return int(exp)
+ return None
+
+ def _decode_jwt_claims(self, token: str) -> Dict[str, Any]:
+ try:
+ parts = token.split(".")
+ if len(parts) < 2:
+ return {}
+ payload_b64 = parts[1]
+ payload_b64 += "=" * (-len(payload_b64) % 4)
+ payload_bytes = base64.urlsafe_b64decode(payload_b64)
+ return json.loads(payload_bytes.decode("utf-8"))
+ except Exception:
+ return {}
+
+ def _extract_account_id(self, token: Optional[str]) -> Optional[str]:
+ if not token:
+ return None
+ claims = self._decode_jwt_claims(token)
+ auth_claims = claims.get("https://api.openai.com/auth")
+ if isinstance(auth_claims, dict):
+ account_id = auth_claims.get("chatgpt_account_id")
+ if isinstance(account_id, str) and account_id:
+ return account_id
+ return None
+
+ def _login_device_code(self) -> Dict[str, str]:
+ cooldown_remaining = self._get_device_code_cooldown_remaining(
+ self._read_auth_file()
+ )
+ if cooldown_remaining > 0:
+ token = self._wait_for_access_token(cooldown_remaining)
+ if token:
+ return {"access_token": token}
+
+ device_code = self._request_device_code()
+ self._record_device_code_request()
+ print( # noqa: T201
+ "Sign in with ChatGPT using device code:\n"
+ f"1) Visit {CHATGPT_DEVICE_VERIFY_URL}\n"
+ f"2) Enter code: {device_code['user_code']}\n"
+ "Device codes are a common phishing target. Never share this code.",
+ flush=True,
+ )
+ auth_code = self._poll_for_authorization_code(device_code)
+ tokens = self._exchange_code_for_tokens(auth_code)
+ auth_data = self._build_auth_record(tokens)
+ self._write_auth_file(auth_data)
+ return tokens
+
+ def _request_device_code(self) -> Dict[str, str]:
+ try:
+ client = _get_httpx_client()
+ resp = client.post(
+ CHATGPT_DEVICE_CODE_URL,
+ json={"client_id": CHATGPT_CLIENT_ID},
+ )
+ resp.raise_for_status()
+ data = resp.json()
+ except httpx.HTTPStatusError as exc:
+ raise GetDeviceCodeError(
+ message=f"Failed to request device code: {exc}",
+ status_code=exc.response.status_code,
+ )
+ except Exception as exc:
+ raise GetDeviceCodeError(
+ message=f"Failed to request device code: {exc}",
+ status_code=400,
+ )
+
+ device_auth_id = data.get("device_auth_id")
+ user_code = data.get("user_code") or data.get("usercode")
+ interval = data.get("interval")
+ if not device_auth_id or not user_code:
+ raise GetDeviceCodeError(
+ message=f"Device code response missing fields: {data}",
+ status_code=400,
+ )
+ return {
+ "device_auth_id": device_auth_id,
+ "user_code": user_code,
+ "interval": str(interval or "5"),
+ }
+
+ def _poll_for_authorization_code(self, device_code: Dict[str, str]) -> Dict[str, str]:
+ client = _get_httpx_client()
+ interval = int(device_code.get("interval", "5"))
+ start_time = time.time()
+ while time.time() - start_time < DEVICE_CODE_TIMEOUT_SECONDS:
+ try:
+ resp = client.post(
+ CHATGPT_DEVICE_TOKEN_URL,
+ json={
+ "device_auth_id": device_code["device_auth_id"],
+ "user_code": device_code["user_code"],
+ },
+ )
+ if resp.status_code == 200:
+ data = resp.json()
+ if all(
+ key in data
+ for key in (
+ "authorization_code",
+ "code_challenge",
+ "code_verifier",
+ )
+ ):
+ return data
+ if resp.status_code in (403, 404):
+ time.sleep(max(interval, DEVICE_CODE_POLL_SLEEP_SECONDS))
+ continue
+ resp.raise_for_status()
+ except httpx.HTTPStatusError as exc:
+ status_code = exc.response.status_code if exc.response else None
+ if status_code in (403, 404):
+ time.sleep(max(interval, DEVICE_CODE_POLL_SLEEP_SECONDS))
+ continue
+ raise GetAccessTokenError(
+ message=f"Polling failed: {exc}",
+ status_code=exc.response.status_code,
+ )
+ except Exception as exc:
+ raise GetAccessTokenError(
+ message=f"Polling failed: {exc}",
+ status_code=400,
+ )
+ time.sleep(max(interval, DEVICE_CODE_POLL_SLEEP_SECONDS))
+
+ raise GetAccessTokenError(
+ message="Timed out waiting for device authorization",
+ status_code=408,
+ )
+
+ def _exchange_code_for_tokens(self, code_data: Dict[str, str]) -> Dict[str, str]:
+ try:
+ client = _get_httpx_client()
+ redirect_uri = f"{CHATGPT_AUTH_BASE}/deviceauth/callback"
+ body = (
+ "grant_type=authorization_code"
+ f"&code={code_data['authorization_code']}"
+ f"&redirect_uri={redirect_uri}"
+ f"&client_id={CHATGPT_CLIENT_ID}"
+ f"&code_verifier={code_data['code_verifier']}"
+ )
+ resp = client.post(
+ CHATGPT_OAUTH_TOKEN_URL,
+ headers={"Content-Type": "application/x-www-form-urlencoded"},
+ content=body,
+ )
+ resp.raise_for_status()
+ data = resp.json()
+ except httpx.HTTPStatusError as exc:
+ raise GetAccessTokenError(
+ message=f"Token exchange failed: {exc}",
+ status_code=exc.response.status_code,
+ )
+ except Exception as exc:
+ raise GetAccessTokenError(
+ message=f"Token exchange failed: {exc}",
+ status_code=400,
+ )
+
+ if not all(key in data for key in ("access_token", "refresh_token", "id_token")):
+ raise GetAccessTokenError(
+ message=f"Token exchange response missing fields: {data}",
+ status_code=400,
+ )
+ return {
+ "access_token": data["access_token"],
+ "refresh_token": data["refresh_token"],
+ "id_token": data["id_token"],
+ }
+
+ def _refresh_tokens(self, refresh_token: str) -> Dict[str, str]:
+ try:
+ client = _get_httpx_client()
+ resp = client.post(
+ CHATGPT_OAUTH_TOKEN_URL,
+ json={
+ "client_id": CHATGPT_CLIENT_ID,
+ "grant_type": "refresh_token",
+ "refresh_token": refresh_token,
+ "scope": "openid profile email",
+ },
+ )
+ resp.raise_for_status()
+ data = resp.json()
+ except httpx.HTTPStatusError as exc:
+ raise RefreshAccessTokenError(
+ message=f"Refresh token failed: {exc}",
+ status_code=exc.response.status_code,
+ )
+ except Exception as exc:
+ raise RefreshAccessTokenError(
+ message=f"Refresh token failed: {exc}",
+ status_code=400,
+ )
+
+ access_token = data.get("access_token")
+ id_token = data.get("id_token")
+ if not access_token or not id_token:
+ raise RefreshAccessTokenError(
+ message=f"Refresh response missing fields: {data}",
+ status_code=400,
+ )
+
+ refreshed = {
+ "access_token": access_token,
+ "refresh_token": data.get("refresh_token", refresh_token),
+ "id_token": id_token,
+ }
+ auth_data = self._build_auth_record(refreshed)
+ self._write_auth_file(auth_data)
+ return refreshed
+
+ def _build_auth_record(self, tokens: Dict[str, str]) -> Dict[str, Any]:
+ access_token = tokens.get("access_token")
+ id_token = tokens.get("id_token")
+ expires_at = self._get_expires_at(access_token) if access_token else None
+ account_id = self._extract_account_id(id_token or access_token)
+ return {
+ "access_token": access_token,
+ "refresh_token": tokens.get("refresh_token"),
+ "id_token": id_token,
+ "expires_at": expires_at,
+ "account_id": account_id,
+ }
+
+ def _get_device_code_cooldown_remaining(
+ self, auth_data: Optional[Dict[str, Any]]
+ ) -> float:
+ if not auth_data:
+ return 0.0
+ requested_at = auth_data.get("device_code_requested_at")
+ if not isinstance(requested_at, (int, float, str)):
+ return 0.0
+ try:
+ requested_at = float(requested_at)
+ except (TypeError, ValueError):
+ return 0.0
+ elapsed = time.time() - requested_at
+ remaining = DEVICE_CODE_COOLDOWN_SECONDS - elapsed
+ return max(0.0, remaining)
+
+ def _record_device_code_request(self) -> None:
+ auth_data = self._read_auth_file() or {}
+ auth_data["device_code_requested_at"] = time.time()
+ self._write_auth_file(auth_data)
+
+ def _wait_for_access_token(self, timeout_seconds: float) -> Optional[str]:
+ deadline = time.time() + timeout_seconds
+ while time.time() < deadline:
+ auth_data = self._read_auth_file()
+ if auth_data:
+ access_token = auth_data.get("access_token")
+ if access_token and not self._is_token_expired(
+ auth_data, access_token
+ ):
+ return access_token
+ sleep_for = min(DEVICE_CODE_POLL_SLEEP_SECONDS, max(0.0, deadline - time.time()))
+ if sleep_for <= 0:
+ break
+ time.sleep(sleep_for)
+ return None
diff --git a/litellm/llms/chatgpt/chat/transformation.py b/litellm/llms/chatgpt/chat/transformation.py
new file mode 100644
index 00000000000..2db5eb3c58d
--- /dev/null
+++ b/litellm/llms/chatgpt/chat/transformation.py
@@ -0,0 +1,75 @@
+from typing import List, Optional, Tuple
+
+from litellm.exceptions import AuthenticationError
+from litellm.llms.openai.openai import OpenAIConfig
+from litellm.types.llms.openai import AllMessageValues
+
+from ..authenticator import Authenticator
+from ..common_utils import (
+ GetAccessTokenError,
+ ensure_chatgpt_session_id,
+ get_chatgpt_default_headers,
+)
+
+
+class ChatGPTConfig(OpenAIConfig):
+ def __init__(
+ self,
+ api_key: Optional[str] = None,
+ api_base: Optional[str] = None,
+ custom_llm_provider: str = "openai",
+ ) -> None:
+ super().__init__()
+ self.authenticator = Authenticator()
+
+ def _get_openai_compatible_provider_info(
+ self,
+ model: str,
+ api_base: Optional[str],
+ api_key: Optional[str],
+ custom_llm_provider: str,
+ ) -> Tuple[Optional[str], Optional[str], str]:
+ dynamic_api_base = self.authenticator.get_api_base()
+ try:
+ dynamic_api_key = self.authenticator.get_access_token()
+ except GetAccessTokenError as e:
+ raise AuthenticationError(
+ model=model,
+ llm_provider=custom_llm_provider,
+ message=str(e),
+ )
+ return dynamic_api_base, dynamic_api_key, custom_llm_provider
+
+ def validate_environment(
+ self,
+ headers: dict,
+ model: str,
+ messages: List[AllMessageValues],
+ optional_params: dict,
+ litellm_params: dict,
+ api_key: Optional[str] = None,
+ api_base: Optional[str] = None,
+ ) -> dict:
+ validated_headers = super().validate_environment(
+ headers, model, messages, optional_params, litellm_params, api_key, api_base
+ )
+
+ account_id = self.authenticator.get_account_id()
+ session_id = ensure_chatgpt_session_id(litellm_params)
+ default_headers = get_chatgpt_default_headers(
+ api_key or "", account_id, session_id
+ )
+ return {**default_headers, **validated_headers}
+
+ def map_openai_params(
+ self,
+ non_default_params: dict,
+ optional_params: dict,
+ model: str,
+ drop_params: bool,
+ ) -> dict:
+ optional_params = super().map_openai_params(
+ non_default_params, optional_params, model, drop_params
+ )
+ optional_params.setdefault("stream", False)
+ return optional_params
diff --git a/litellm/llms/chatgpt/common_utils.py b/litellm/llms/chatgpt/common_utils.py
new file mode 100644
index 00000000000..d80487cde24
--- /dev/null
+++ b/litellm/llms/chatgpt/common_utils.py
@@ -0,0 +1,301 @@
+"""
+Constants and helpers for ChatGPT subscription OAuth.
+"""
+import os
+import platform
+from typing import Any, Optional, Union
+from uuid import uuid4
+
+import httpx
+
+from litellm.llms.base_llm.chat.transformation import BaseLLMException
+
+# OAuth + API constants (derived from openai/codex)
+CHATGPT_AUTH_BASE = "https://auth.openai.com"
+CHATGPT_DEVICE_CODE_URL = f"{CHATGPT_AUTH_BASE}/api/accounts/deviceauth/usercode"
+CHATGPT_DEVICE_TOKEN_URL = f"{CHATGPT_AUTH_BASE}/api/accounts/deviceauth/token"
+CHATGPT_OAUTH_TOKEN_URL = f"{CHATGPT_AUTH_BASE}/oauth/token"
+CHATGPT_DEVICE_VERIFY_URL = f"{CHATGPT_AUTH_BASE}/codex/device"
+CHATGPT_API_BASE = "https://chatgpt.com/backend-api/codex"
+CHATGPT_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
+
+DEFAULT_ORIGINATOR = "codex_cli_rs"
+DEFAULT_USER_AGENT = "codex_cli_rs/0.0.0 (Unknown 0; unknown) unknown"
+CHATGPT_DEFAULT_INSTRUCTIONS = """You are Codex, based on GPT-5. You are running as a coding agent in the Codex CLI on a user's computer.
+
+## General
+
+- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.)
+
+## Editing constraints
+
+- Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
+- Add succinct code comments that explain what is going on if code is not self-explanatory. You should not add comments like "Assigns the value to the variable", but a brief comment might be useful ahead of a complex code block that the user would otherwise have to spend time parsing out. Usage of these comments should be rare.
+- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).
+- You may be in a dirty git worktree.
+ * NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.
+ * If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes.
+ * If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them.
+ * If the changes are in unrelated files, just ignore them and don't revert them.
+- Do not amend a commit unless explicitly requested to do so.
+- While you are working, you might notice unexpected changes that you didn't make. If this happens, STOP IMMEDIATELY and ask the user how they would like to proceed.
+- **NEVER** use destructive commands like `git reset --hard` or `git checkout --` unless specifically requested or approved by the user.
+
+## Plan tool
+
+When using the planning tool:
+- Skip using the planning tool for straightforward tasks (roughly the easiest 25%).
+- Do not make single-step plans.
+- When you made a plan, update it after having performed one of the sub-tasks that you shared on the plan.
+
+## Special user requests
+
+- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.
+- If the user asks for a "review", default to a code review mindset: prioritise identifying bugs, risks, behavioural regressions, and missing tests. Findings must be the primary focus of the response - keep summaries or overviews brief and only after enumerating the issues. Present findings first (ordered by severity with file/line references), follow with open questions or assumptions, and offer a change-summary only as a secondary detail. If no findings are discovered, state that explicitly and mention any residual risks or testing gaps.
+
+## Frontend tasks
+When doing frontend design tasks, avoid collapsing into "AI slop" or safe, average-looking layouts.
+Aim for interfaces that feel intentional, bold, and a bit surprising.
+- Typography: Use expressive, purposeful fonts and avoid default stacks (Inter, Roboto, Arial, system).
+- Color & Look: Choose a clear visual direction; define CSS variables; avoid purple-on-white defaults. No purple bias or dark mode bias.
+- Motion: Use a few meaningful animations (page-load, staggered reveals) instead of generic micro-motions.
+- Background: Don't rely on flat, single-color backgrounds; use gradients, shapes, or subtle patterns to build atmosphere.
+- Overall: Avoid boilerplate layouts and interchangeable UI patterns. Vary themes, type families, and visual languages across outputs.
+- Ensure the page loads properly on both desktop and mobile
+
+Exception: If working within an existing website or design system, preserve the established patterns, structure, and visual language.
+
+## Presenting your work and final message
+
+You are producing plain text that will later be styled by the CLI. Follow these rules exactly. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value.
+
+- Default: be very concise; friendly coding teammate tone.
+- Ask only when needed; suggest ideas; mirror the user's style.
+- For substantial work, summarize clearly; follow final-answer formatting.
+- Skip heavy formatting for simple confirmations.
+- Don't dump large files you've written; reference paths only.
+- No "save/copy this file" - User is on the same machine.
+- Offer logical next steps (tests, commits, build) briefly; add verify steps if you couldn't do something.
+- For code changes:
+ * Lead with a quick explanation of the change, and then give more details on the context covering where and why a change was made. Do not start this explanation with "summary", just jump right in.
+ * If there are natural next steps the user may want to take, suggest them at the end of your response. Do not make suggestions if there are no natural next steps.
+ * When suggesting multiple options, use numeric lists for the suggestions so the user can quickly respond with a single number.
+- The user does not command execution outputs. When asked to show the output of a command (e.g. `git show`), relay the important details in your answer or summarize the key lines so the user understands the result.
+
+### Final answer structure and style guidelines
+
+- Plain text; CLI handles styling. Use structure only when it helps scanability.
+- Headers: optional; short Title Case (1-3 words) wrapped in **...**; no blank line before the first bullet; add only if they truly help.
+- Bullets: use - ; merge related points; keep to one line when possible; 4-6 per list ordered by importance; keep phrasing consistent.
+- Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **.
+- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible.
+- Structure: group related bullets; order sections general -> specific -> supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task.
+- Tone: collaborative, concise, factual; present tense, active voice; self-contained; no "above/below"; parallel wording.
+- Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short--wrap/reformat if long; avoid naming formatting styles in answers.
+- Adaptation: code explanations -> precise, structured with code refs; simple tasks -> lead with outcome; big changes -> logical walkthrough + rationale + next actions; casual one-offs -> plain sentences, no headers/bullets.
+- File References: When referencing files in your response follow the below rules:
+ * Use inline code to make file paths clickable.
+ * Each reference should have a stand alone path. Even if it's the same file.
+ * Accepted: absolute, workspace-relative, a/ or b/ diff prefixes, or bare filename/suffix.
+ * Optionally include line/column (1-based): :line[:column] or #Lline[Ccolumn] (column defaults to 1).
+ * Do not use URIs like file://, vscode://, or https://.
+ * Do not provide range of lines
+ * Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\\repo\\project\\main.rs:12:5
+"""
+
+
+class ChatGPTAuthError(BaseLLMException):
+ def __init__(
+ self,
+ status_code,
+ message,
+ request: Optional[httpx.Request] = None,
+ response: Optional[httpx.Response] = None,
+ headers: Optional[Union[httpx.Headers, dict]] = None,
+ body: Optional[dict] = None,
+ ):
+ super().__init__(
+ status_code=status_code,
+ message=message,
+ request=request,
+ response=response,
+ headers=headers,
+ body=body,
+ )
+
+
+class GetDeviceCodeError(ChatGPTAuthError):
+ pass
+
+
+class GetAccessTokenError(ChatGPTAuthError):
+ pass
+
+
+class RefreshAccessTokenError(ChatGPTAuthError):
+ pass
+
+
+def _safe_header_value(value: str) -> str:
+ if not value:
+ return ""
+ return "".join(ch if 32 <= ord(ch) <= 126 else "_" for ch in value)
+
+
+def _sanitize_user_agent_token(value: str) -> str:
+ if not value:
+ return ""
+ return "".join(
+ ch if (ch.isalnum() or ch in "-_./") else "_" for ch in value
+ )
+
+
+def _terminal_user_agent() -> str:
+ term_program = os.getenv("TERM_PROGRAM")
+ if term_program:
+ version = os.getenv("TERM_PROGRAM_VERSION")
+ token = f"{term_program}/{version}" if version else term_program
+ return _sanitize_user_agent_token(token) or "unknown"
+
+ wezterm_version = os.getenv("WEZTERM_VERSION")
+ if wezterm_version is not None:
+ token = (
+ f"WezTerm/{wezterm_version}" if wezterm_version else "WezTerm"
+ )
+ return _sanitize_user_agent_token(token) or "WezTerm"
+
+ if (
+ os.getenv("ITERM_SESSION_ID")
+ or os.getenv("ITERM_PROFILE")
+ or os.getenv("ITERM_PROFILE_NAME")
+ ):
+ return "iTerm.app"
+
+ if os.getenv("TERM_SESSION_ID"):
+ return "Apple_Terminal"
+
+ if os.getenv("KITTY_WINDOW_ID") or "kitty" in (os.getenv("TERM") or ""):
+ return "kitty"
+
+ if os.getenv("ALACRITTY_SOCKET") or os.getenv("TERM") == "alacritty":
+ return "Alacritty"
+
+ konsole_version = os.getenv("KONSOLE_VERSION")
+ if konsole_version is not None:
+ token = (
+ f"Konsole/{konsole_version}" if konsole_version else "Konsole"
+ )
+ return _sanitize_user_agent_token(token) or "Konsole"
+
+ if os.getenv("GNOME_TERMINAL_SCREEN"):
+ return "gnome-terminal"
+
+ vte_version = os.getenv("VTE_VERSION")
+ if vte_version is not None:
+ token = f"VTE/{vte_version}" if vte_version else "VTE"
+ return _sanitize_user_agent_token(token) or "VTE"
+
+ if os.getenv("WT_SESSION"):
+ return "WindowsTerminal"
+
+ term = os.getenv("TERM")
+ if term:
+ return _sanitize_user_agent_token(term) or "unknown"
+
+ return "unknown"
+
+
+def _get_litellm_version() -> str:
+ try:
+ from importlib.metadata import version
+
+ return version("litellm")
+ except Exception:
+ return "0.0.0"
+
+
+def get_chatgpt_originator() -> str:
+ originator = os.getenv("CHATGPT_ORIGINATOR") or DEFAULT_ORIGINATOR
+ return _safe_header_value(originator) or DEFAULT_ORIGINATOR
+
+
+def get_chatgpt_user_agent(originator: str) -> str:
+ override = os.getenv("CHATGPT_USER_AGENT")
+ if override:
+ return _safe_header_value(override) or DEFAULT_USER_AGENT
+ version = _get_litellm_version()
+ os_type = platform.system() or "Unknown"
+ os_version = platform.release() or "0"
+ arch = platform.machine() or "unknown"
+ terminal_ua = _terminal_user_agent()
+ suffix = os.getenv("CHATGPT_USER_AGENT_SUFFIX", "").strip()
+ suffix = f" ({suffix})" if suffix else ""
+ candidate = (
+ f"{originator}/{version} ({os_type} {os_version}; {arch}) {terminal_ua}{suffix}"
+ )
+ return _safe_header_value(candidate) or DEFAULT_USER_AGENT
+
+
+def get_chatgpt_default_headers(
+ access_token: str,
+ account_id: Optional[str],
+ session_id: Optional[str] = None,
+) -> dict:
+ originator = get_chatgpt_originator()
+ user_agent = get_chatgpt_user_agent(originator)
+ headers = {
+ "Authorization": f"Bearer {access_token}",
+ "content-type": "application/json",
+ "accept": "text/event-stream",
+ "originator": originator,
+ "user-agent": user_agent,
+ }
+ if session_id:
+ headers["session_id"] = session_id
+ if account_id:
+ headers["ChatGPT-Account-Id"] = account_id
+ return headers
+
+
+def get_chatgpt_default_instructions() -> str:
+ return os.getenv("CHATGPT_DEFAULT_INSTRUCTIONS") or CHATGPT_DEFAULT_INSTRUCTIONS
+
+
+def _normalize_litellm_params(litellm_params: Optional[Any]) -> dict:
+ if litellm_params is None:
+ return {}
+ if isinstance(litellm_params, dict):
+ return litellm_params
+ if hasattr(litellm_params, "model_dump"):
+ try:
+ return litellm_params.model_dump()
+ except Exception:
+ return {}
+ if hasattr(litellm_params, "dict"):
+ try:
+ return litellm_params.dict()
+ except Exception:
+ return {}
+ return {}
+
+
+def get_chatgpt_session_id(litellm_params: Optional[Any]) -> Optional[str]:
+ params = _normalize_litellm_params(litellm_params)
+ for key in ("litellm_session_id", "session_id"):
+ value = params.get(key)
+ if value:
+ return str(value)
+ metadata = params.get("metadata")
+ if isinstance(metadata, dict):
+ value = metadata.get("session_id")
+ if value:
+ return str(value)
+ for key in ("litellm_trace_id", "litellm_call_id"):
+ value = params.get(key)
+ if value:
+ return str(value)
+ return None
+
+
+def ensure_chatgpt_session_id(litellm_params: Optional[Any]) -> str:
+ return get_chatgpt_session_id(litellm_params) or str(uuid4())
diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.py
new file mode 100644
index 00000000000..0ce24f63a89
--- /dev/null
+++ b/litellm/llms/chatgpt/responses/transformation.py
@@ -0,0 +1,191 @@
+import json
+from typing import Any, Optional
+
+from litellm.exceptions import AuthenticationError
+from litellm.constants import STREAM_SSE_DONE_STRING
+from litellm.litellm_core_utils.core_helpers import process_response_headers
+from litellm.llms.openai.common_utils import OpenAIError
+from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
+from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
+ _safe_convert_created_field,
+)
+from litellm.types.llms.openai import (
+ ResponsesAPIResponse,
+ ResponsesAPIStreamEvents,
+)
+from litellm.types.router import GenericLiteLLMParams
+from litellm.types.utils import LlmProviders
+from litellm.utils import CustomStreamWrapper
+
+from ..authenticator import Authenticator
+from ..common_utils import (
+ CHATGPT_API_BASE,
+ GetAccessTokenError,
+ ensure_chatgpt_session_id,
+ get_chatgpt_default_headers,
+ get_chatgpt_default_instructions,
+)
+
+
+class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig):
+ def __init__(self) -> None:
+ super().__init__()
+ self.authenticator = Authenticator()
+
+ @property
+ def custom_llm_provider(self) -> LlmProviders:
+ return LlmProviders.CHATGPT
+
+ def validate_environment(
+ self,
+ headers: dict,
+ model: str,
+ litellm_params: Optional[GenericLiteLLMParams],
+ ) -> dict:
+ try:
+ access_token = self.authenticator.get_access_token()
+ except GetAccessTokenError as e:
+ raise AuthenticationError(
+ model=model,
+ llm_provider="chatgpt",
+ message=str(e),
+ )
+
+ account_id = self.authenticator.get_account_id()
+ session_id = ensure_chatgpt_session_id(litellm_params)
+ default_headers = get_chatgpt_default_headers(
+ access_token, account_id, session_id
+ )
+ return {**default_headers, **headers}
+
+ def transform_responses_api_request(
+ self,
+ model: str,
+ input: Any,
+ response_api_optional_request_params: dict,
+ litellm_params: GenericLiteLLMParams,
+ headers: dict,
+ ) -> dict:
+ request = super().transform_responses_api_request(
+ model,
+ input,
+ response_api_optional_request_params,
+ litellm_params,
+ headers,
+ )
+ request.pop("max_output_tokens", None)
+ request.pop("max_tokens", None)
+ request.pop("max_completion_tokens", None)
+ request.pop("metadata", None)
+ base_instructions = get_chatgpt_default_instructions()
+ existing_instructions = request.get("instructions")
+ if existing_instructions:
+ if base_instructions not in existing_instructions:
+ request["instructions"] = (
+ f"{base_instructions}\n\n{existing_instructions}"
+ )
+ else:
+ request["instructions"] = base_instructions
+ request["store"] = False
+ request["stream"] = True
+ include = list(request.get("include") or [])
+ if "reasoning.encrypted_content" not in include:
+ include.append("reasoning.encrypted_content")
+ request["include"] = include
+ return request
+
+ def transform_response_api_response(
+ self,
+ model: str,
+ raw_response: Any,
+ logging_obj: Any,
+ ):
+ content_type = (raw_response.headers or {}).get("content-type", "")
+ body_text = raw_response.text or ""
+ if "text/event-stream" not in content_type.lower():
+ trimmed_body = body_text.lstrip()
+ if not (
+ trimmed_body.startswith("event:")
+ or trimmed_body.startswith("data:")
+ or "\nevent:" in body_text
+ or "\ndata:" in body_text
+ ):
+ return super().transform_response_api_response(
+ model=model,
+ raw_response=raw_response,
+ logging_obj=logging_obj,
+ )
+
+ logging_obj.post_call(
+ original_response=raw_response.text,
+ additional_args={"complete_input_dict": {}},
+ )
+
+ completed_response = None
+ error_message = None
+ for chunk in body_text.splitlines():
+ stripped_chunk = CustomStreamWrapper._strip_sse_data_from_chunk(chunk)
+ if not stripped_chunk:
+ continue
+ stripped_chunk = stripped_chunk.strip()
+ if not stripped_chunk:
+ continue
+ if stripped_chunk == STREAM_SSE_DONE_STRING:
+ break
+ try:
+ parsed_chunk = json.loads(stripped_chunk)
+ except json.JSONDecodeError:
+ continue
+ if not isinstance(parsed_chunk, dict):
+ continue
+ event_type = parsed_chunk.get("type")
+ if event_type == ResponsesAPIStreamEvents.RESPONSE_COMPLETED:
+ response_payload = parsed_chunk.get("response")
+ if isinstance(response_payload, dict):
+ response_payload = dict(response_payload)
+ if "created_at" in response_payload:
+ response_payload["created_at"] = _safe_convert_created_field(
+ response_payload["created_at"]
+ )
+ try:
+ completed_response = ResponsesAPIResponse(**response_payload)
+ except Exception:
+ completed_response = ResponsesAPIResponse.model_construct(
+ **response_payload
+ )
+ break
+ if event_type in (
+ ResponsesAPIStreamEvents.RESPONSE_FAILED,
+ ResponsesAPIStreamEvents.ERROR,
+ ):
+ error_obj = parsed_chunk.get("error") or (
+ parsed_chunk.get("response") or {}
+ ).get("error")
+ if error_obj is not None:
+ if isinstance(error_obj, dict):
+ error_message = error_obj.get("message") or str(error_obj)
+ else:
+ error_message = str(error_obj)
+
+ if completed_response is None:
+ raise OpenAIError(
+ message=error_message or raw_response.text,
+ status_code=raw_response.status_code,
+ )
+
+ raw_headers = dict(raw_response.headers)
+ processed_headers = process_response_headers(raw_headers)
+ if not hasattr(completed_response, "_hidden_params"):
+ setattr(completed_response, "_hidden_params", {})
+ completed_response._hidden_params["additional_headers"] = processed_headers
+ completed_response._hidden_params["headers"] = raw_headers
+ return completed_response
+
+ def get_complete_url(
+ self,
+ api_base: Optional[str],
+ litellm_params: dict,
+ ) -> str:
+ api_base = api_base or self.authenticator.get_api_base() or CHATGPT_API_BASE
+ api_base = api_base.rstrip("/")
+ return f"{api_base}/responses"
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index 470d598a25f..cd4187ff779 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -15932,6 +15932,64 @@
"max_tokens": 8191,
"mode": "embedding"
},
+ "chatgpt/gpt-5.2-codex": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 128000,
+ "max_tokens": 128000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.1-codex-max": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 128000,
+ "max_tokens": 128000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.1-codex-mini": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 64000,
+ "max_tokens": 64000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.2": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 64000,
+ "max_tokens": 64000,
+ "mode": "chat",
+ "supported_endpoints": [
+ "/v1/chat/completions",
+ "/v1/messages",
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
"gigachat/GigaChat-2-Lite": {
"input_cost_per_token": 0.0,
"litellm_provider": "gigachat",
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index 8c30e4d7e80..fd83b7e79f0 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -2958,6 +2958,7 @@ GenericBudgetConfigType = Dict[str, BudgetConfig]
class LlmProviders(str, Enum):
OPENAI = "openai"
+ CHATGPT = "chatgpt"
OPENAI_LIKE = "openai_like" # embedding only
JINA_AI = "jina_ai"
XAI = "xai"
diff --git a/litellm/utils.py b/litellm/utils.py
index ac194e4f339..f5b5c50468f 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -7659,6 +7659,7 @@ class ProviderConfigManager:
LlmProviders.GITHUB: (lambda: litellm.GithubChatConfig(), False),
LlmProviders.COMPACTIFAI: (lambda: litellm.CompactifAIChatConfig(), False),
LlmProviders.GITHUB_COPILOT: (lambda: litellm.GithubCopilotConfig(), False),
+ LlmProviders.CHATGPT: (lambda: litellm.ChatGPTConfig(), False),
LlmProviders.GIGACHAT: (lambda: litellm.GigaChatConfig(), False),
LlmProviders.RAGFLOW: (lambda: litellm.RAGFlowConfig(), False),
LlmProviders.CUSTOM: (lambda: litellm.OpenAILikeChatConfig(), False),
@@ -8028,6 +8029,8 @@ class ProviderConfigManager:
return litellm.XAIResponsesAPIConfig()
elif litellm.LlmProviders.GITHUB_COPILOT == provider:
return litellm.GithubCopilotResponsesAPIConfig()
+ elif litellm.LlmProviders.CHATGPT == provider:
+ return litellm.ChatGPTResponsesAPIConfig()
elif litellm.LlmProviders.LITELLM_PROXY == provider:
return litellm.LiteLLMProxyResponsesAPIConfig()
elif litellm.LlmProviders.MANUS == provider:
diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json
index 470d598a25f..c846edb85cb 100644
--- a/model_prices_and_context_window.json
+++ b/model_prices_and_context_window.json
@@ -15932,6 +15932,63 @@
"max_tokens": 8191,
"mode": "embedding"
},
+ "chatgpt/gpt-5.2-codex": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 128000,
+ "max_tokens": 128000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.2": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 64000,
+ "max_tokens": 64000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/chat/completions",
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.1-codex-max": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 128000,
+ "max_tokens": 128000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
+ "chatgpt/gpt-5.1-codex-mini": {
+ "litellm_provider": "chatgpt",
+ "max_input_tokens": 128000,
+ "max_output_tokens": 64000,
+ "max_tokens": 64000,
+ "mode": "responses",
+ "supported_endpoints": [
+ "/v1/responses"
+ ],
+ "supports_function_calling": true,
+ "supports_parallel_function_calling": true,
+ "supports_response_schema": true,
+ "supports_vision": true
+ },
"gigachat/GigaChat-2-Lite": {
"input_cost_per_token": 0.0,
"litellm_provider": "gigachat",
diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json
index db11ec8a0de..b7892a67a17 100644
--- a/provider_endpoints_support.json
+++ b/provider_endpoints_support.json
@@ -919,6 +919,24 @@
"interactions": true
}
},
+ "chatgpt": {
+ "display_name": "ChatGPT Subscription (`chatgpt`)",
+ "url": "https://docs.litellm.ai/docs/providers/chatgpt",
+ "endpoints": {
+ "chat_completions": true,
+ "messages": true,
+ "responses": true,
+ "embeddings": false,
+ "image_generations": false,
+ "audio_transcriptions": false,
+ "audio_speech": false,
+ "moderations": false,
+ "batches": false,
+ "rerank": false,
+ "a2a": false,
+ "interactions": false
+ }
+ },
"github": {
"display_name": "GitHub Models (`github`)",
"url": "https://docs.litellm.ai/docs/providers/github",
diff --git a/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py
new file mode 100644
index 00000000000..bec748d8dc8
--- /dev/null
+++ b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py
@@ -0,0 +1,125 @@
+"""
+Tests for ChatGPT subscription Responses API transformation
+
+Source: litellm/llms/chatgpt/responses/transformation.py
+"""
+import json
+import os
+import sys
+from unittest.mock import MagicMock, patch
+
+import httpx
+
+sys.path.insert(0, os.path.abspath("../../../../.."))
+
+from litellm.types.router import GenericLiteLLMParams
+from litellm.types.utils import LlmProviders
+from litellm.utils import ProviderConfigManager
+from litellm.llms.chatgpt.responses.transformation import ChatGPTResponsesAPIConfig
+
+
+class TestChatGPTResponsesAPITransformation:
+ def test_chatgpt_provider_config_registration(self):
+ config = ProviderConfigManager.get_provider_responses_api_config(
+ model="chatgpt/gpt-5.2",
+ provider=LlmProviders.CHATGPT,
+ )
+
+ assert config is not None
+ assert isinstance(config, ChatGPTResponsesAPIConfig)
+ assert config.custom_llm_provider == LlmProviders.CHATGPT
+
+ @patch("litellm.llms.chatgpt.responses.transformation.Authenticator")
+ def test_chatgpt_responses_endpoint_url(self, mock_authenticator_class):
+ mock_auth_instance = MagicMock()
+ mock_auth_instance.get_api_base.return_value = "https://chatgpt.example.com"
+ mock_authenticator_class.return_value = mock_auth_instance
+
+ config = ChatGPTResponsesAPIConfig()
+
+ url = config.get_complete_url(api_base=None, litellm_params={})
+ assert url == "https://chatgpt.example.com/responses"
+
+ custom_url = config.get_complete_url(
+ api_base="https://custom.chatgpt.com", litellm_params={}
+ )
+ assert custom_url == "https://custom.chatgpt.com/responses"
+
+ url_with_slash = config.get_complete_url(
+ api_base="https://chatgpt.example.com/", litellm_params={}
+ )
+ assert url_with_slash == "https://chatgpt.example.com/responses"
+
+ @patch("litellm.llms.chatgpt.responses.transformation.Authenticator")
+ def test_validate_environment_headers(self, mock_authenticator_class):
+ mock_auth_instance = MagicMock()
+ mock_auth_instance.get_access_token.return_value = "access-123"
+ mock_auth_instance.get_account_id.return_value = "acct-123"
+ mock_authenticator_class.return_value = mock_auth_instance
+
+ config = ChatGPTResponsesAPIConfig()
+ litellm_params = GenericLiteLLMParams(litellm_session_id="session-123")
+ headers = config.validate_environment(
+ headers={"originator": "custom-origin"},
+ model="gpt-5.2",
+ litellm_params=litellm_params,
+ )
+
+ assert headers["Authorization"] == "Bearer access-123"
+ assert headers["ChatGPT-Account-Id"] == "acct-123"
+ assert headers["originator"] == "custom-origin"
+ assert headers["content-type"] == "application/json"
+ assert headers["accept"] == "text/event-stream"
+ assert headers["session_id"] == "session-123"
+
+ def test_chatgpt_forces_streaming_and_reasoning_include(self):
+ config = ChatGPTResponsesAPIConfig()
+ request = config.transform_responses_api_request(
+ model="chatgpt/gpt-5.2-codex",
+ input="hi",
+ response_api_optional_request_params={},
+ litellm_params=GenericLiteLLMParams(),
+ headers={},
+ )
+
+ assert request["stream"] is True
+ assert "reasoning.encrypted_content" in request["include"]
+ assert request["instructions"].startswith(
+ "You are Codex, based on GPT-5."
+ )
+
+ def test_chatgpt_non_stream_sse_response_parsing(self):
+ config = ChatGPTResponsesAPIConfig()
+ response_payload = {
+ "id": "resp_test",
+ "object": "response",
+ "created_at": 1700000000,
+ "status": "completed",
+ "model": "gpt-5.2-codex",
+ "output": [
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "output_text", "text": "Hello!"}],
+ }
+ ],
+ }
+ sse_body = "\n".join(
+ [
+ f"data: {json.dumps({'type': 'response.completed', 'response': response_payload})}",
+ "data: [DONE]",
+ "",
+ ]
+ )
+ raw_response = httpx.Response(
+ 200, headers={"content-type": "text/event-stream"}, text=sse_body
+ )
+ logging_obj = MagicMock()
+
+ parsed = config.transform_response_api_response(
+ model="chatgpt/gpt-5.2-codex",
+ raw_response=raw_response,
+ logging_obj=logging_obj,
+ )
+
+ assert parsed.output_text == "Hello!"
diff --git a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py
new file mode 100644
index 00000000000..5a4b58e159a
--- /dev/null
+++ b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py
@@ -0,0 +1,68 @@
+import base64
+import json
+import time
+from unittest.mock import mock_open, patch
+
+import pytest
+
+from litellm.llms.chatgpt.authenticator import Authenticator
+
+
+def _make_jwt(payload: dict) -> str:
+ header = {"alg": "none", "typ": "JWT"}
+
+ def _b64(obj: dict) -> str:
+ raw = json.dumps(obj, separators=(",", ":")).encode("utf-8")
+ return base64.urlsafe_b64encode(raw).decode("utf-8").rstrip("=")
+
+ return f"{_b64(header)}.{_b64(payload)}."
+
+
+class TestChatGPTAuthenticator:
+ @pytest.fixture
+ def authenticator(self):
+ with patch("os.path.exists", return_value=True):
+ return Authenticator()
+
+ def test_get_access_token_from_file(self, authenticator):
+ future_time = time.time() + 3600
+ auth_data = json.dumps({"access_token": "token-123", "expires_at": future_time})
+
+ with patch("builtins.open", mock_open(read_data=auth_data)):
+ token = authenticator.get_access_token()
+ assert token == "token-123"
+
+ def test_get_access_token_refresh(self, authenticator):
+ past_time = time.time() - 10
+ auth_data = json.dumps(
+ {
+ "access_token": "token-old",
+ "refresh_token": "refresh-123",
+ "expires_at": past_time,
+ }
+ )
+ refreshed = {
+ "access_token": "token-new",
+ "refresh_token": "refresh-123",
+ "id_token": "id-123",
+ }
+
+ with patch("builtins.open", mock_open(read_data=auth_data)), patch.object(
+ authenticator, "_refresh_tokens", return_value=refreshed
+ ):
+ token = authenticator.get_access_token()
+ assert token == "token-new"
+
+ def test_get_account_id_from_id_token(self, authenticator):
+ id_token = _make_jwt(
+ {"https://api.openai.com/auth": {"chatgpt_account_id": "acct-123"}}
+ )
+ auth_data = json.dumps({"id_token": id_token})
+
+ with patch("builtins.open", mock_open(read_data=auth_data)), patch.object(
+ authenticator, "_write_auth_file"
+ ) as mock_write:
+ account_id = authenticator.get_account_id()
+ assert account_id == "acct-123"
+ mock_write.assert_called_once()
+ assert mock_write.call_args[0][0]["account_id"] == "acct-123"
diff --git a/tests/test_litellm/test_responses_api_bridge_non_stream.py b/tests/test_litellm/test_responses_api_bridge_non_stream.py
new file mode 100644
index 00000000000..8f72debfc5d
--- /dev/null
+++ b/tests/test_litellm/test_responses_api_bridge_non_stream.py
@@ -0,0 +1,44 @@
+from litellm.completion_extras.litellm_responses_transformation.handler import (
+ ResponsesToCompletionBridgeHandler,
+)
+from litellm.types.llms.openai import ResponsesAPIResponse
+
+
+class _CompletedEvent:
+ def __init__(self, response):
+ self.response = response
+
+
+class _FakeResponsesStream:
+ def __init__(self, response):
+ self._emitted = False
+ self._response = response
+ self.completed_response = None
+ self._hidden_params = {"headers": {"x-test": "1"}}
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if not self._emitted:
+ self._emitted = True
+ self.completed_response = _CompletedEvent(self._response)
+ return {"type": "response.completed"}
+ raise StopIteration
+
+
+def test_should_collect_response_from_stream():
+ handler = ResponsesToCompletionBridgeHandler()
+ response = ResponsesAPIResponse.model_construct(
+ id="resp-1",
+ created_at=0,
+ output=[],
+ object="response",
+ model="gpt-5.2",
+ )
+ stream = _FakeResponsesStream(response)
+
+ collected = handler._collect_response_from_stream(stream)
+
+ assert collected.id == "resp-1"
+ assert collected._hidden_params.get("headers") == {"x-test": "1"}