Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🗣️ fix: Keep Provider Error Text on Unclassified Agent Failures (#16034)
Marco Beretta9633edcb888d
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.jsindex 9398181489..d11da05185 100644--- a/api/server/controllers/agents/client.js+++ b/api/server/controllers/agents/client.js@@ -4402,13 +4402,18 @@ class AgentClient extends BaseClient {let run;/** @type {Promise<(TAttachment | null)[] | undefined>} */let memoryPromise;+ const appConfig = this.options.req.config;const terminalRunError = createTerminalRunErrorObserver({+ maxProviderErrorChars: appConfig?.endpoints?.agents?.maxProviderErrorChars,logger,responseMessageId: this.responseMessageId,source: '[api/server/controllers/agents/client.js #sendCompletion]',genericMessage: '[api/server/controllers/agents/client.js #sendCompletion] Unhandled error',+ protectionEnabled: hasModelBoundContentProtection(+ appConfig?.filters,+ appConfig?.messageFilter?.pii,+ ),});- const appConfig = this.options.req.config;const balanceConfig = getBalanceConfig(appConfig);const transactionsConfig = getTransactionsConfig(appConfig);try {@@ -5287,13 +5292,18 @@ class AgentClient extends BaseClient {let config;/** @type {ReturnType<createRun>} */let run;+ const appConfig = this.options.req.config;const terminalRunError = createTerminalRunErrorObserver({+ maxProviderErrorChars: appConfig?.endpoints?.agents?.maxProviderErrorChars,logger,responseMessageId: this.responseMessageId,source: '[api/server/controllers/agents/client.js #resumeCompletion]',genericMessage: '[api/server/controllers/agents/client.js #resumeCompletion] Unhandled error',+ protectionEnabled: hasModelBoundContentProtection(+ appConfig?.filters,+ appConfig?.messageFilter?.pii,+ ),});- const appConfig = this.options.req.config;const balanceConfig = getBalanceConfig(appConfig);const transactionsConfig = getTransactionsConfig(appConfig);try {diff --git a/api/server/controllers/agents/client.test.js b/api/server/controllers/agents/client.test.jsindex 60b8561dbf..82ccb23afd 100644--- a/api/server/controllers/agents/client.test.js+++ b/api/server/controllers/agents/client.test.js@@ -10083,6 +10083,61 @@ describe('AgentClient - resumeCompletion content protection', () => {errorSpy.mockRestore();});+ /** A gateway or privacy proxy states its rejection in its own message and nowhere else, so an+ * unclassified upstream failure carries it exactly as every other failure text does. */+ it.each([undefined, 24, 3000])(+ 'keeps the provider explanation with limit %s on a terminal resumed model failure',+ async (maxProviderErrorChars) => {+ const explanation = '400 Request rejected: this prompt cannot be masked safely';+ const trackTerminalProviderError = (providerError) => {+ mockCreateRun.mockImplementation(async (options) => {+ const tracker = options.modelCallbacks.find(+ (callback) => callback.name === 'librechat-upstream-model-error-tracker',+ );+ return {+ resume: jest.fn(async () => {+ tracker.handleLLMError(providerError, 'resumed-model-run');+ throw providerError;+ }),+ getCalibrationRatio: jest.fn(() => 0),+ };+ });+ };++ trackTerminalProviderError(Object.assign(new Error(explanation), { status: 400 }));+ const context = makeContext(undefined);+ context.options.req.config.endpoints = { agents: { maxProviderErrorChars } };++ await AgentClient.prototype.resumeCompletion.call(context, { resumeValue: {} });++ expect(context.contentParts).toContainEqual({+ type: ContentTypes.ERROR,+ [ContentTypes.ERROR]:+ 'The model provider could not complete this request.\n' ++ JSON.stringify({+ type: 'upstream_model_error',+ status: 400,+ message: explanation.slice(0, maxProviderErrorChars),+ }),+ });++ /** With a policy inspecting the traffic, the body may echo submitted content: status only. */+ trackTerminalProviderError(Object.assign(new Error(explanation), { status: 400 }));+ const protectedContext = makeContext({+ messages: { pii: { fields: ['text'], starterPatterns: ['email'] } },+ });++ await AgentClient.prototype.resumeCompletion.call(protectedContext, { resumeValue: {} });++ expect(protectedContext.contentParts).toContainEqual({+ type: ContentTypes.ERROR,+ [ContentTypes.ERROR]:+ 'The model provider could not complete this request.\n' ++ JSON.stringify({ type: 'upstream_model_error', status: 400 }),+ });+ },+ );+it('preserves provider error detail when content protection is disabled', async () => {const providerMessage = 'Legacy provider detail';mockCreateRun.mockRejectedValue(new Error(providerMessage));diff --git a/api/server/controllers/agents/openai.js b/api/server/controllers/agents/openai.jsindex 6681ba6063..7bcbe4b9fa 100644--- a/api/server/controllers/agents/openai.js+++ b/api/server/controllers/agents/openai.js@@ -380,9 +380,14 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {const responseId = `chatcmpl-${nanoid()}`;const terminalRunError = createTerminalRunErrorObserver({+ maxProviderErrorChars: appConfig?.endpoints?.agents?.maxProviderErrorChars,logger,responseMessageId: responseId,source: '[OpenAI API]',+ protectionEnabled: hasModelBoundContentProtection(+ appConfig?.filters,+ appConfig?.messageFilter?.pii,+ ),});const created = Math.floor(Date.now() / 1000);diff --git a/api/server/controllers/agents/responses.js b/api/server/controllers/agents/responses.jsindex c0b6c8e8b9..24e668576b 100644--- a/api/server/controllers/agents/responses.js+++ b/api/server/controllers/agents/responses.js@@ -629,9 +629,14 @@ const executeResponse = async (envelope, { req, res }) => {// Generate IDsconst responseId = generateResponseId();const terminalRunError = createTerminalRunErrorObserver({+ maxProviderErrorChars: appConfig?.endpoints?.agents?.maxProviderErrorChars,logger,responseMessageId: responseId,source: '[Responses API]',+ protectionEnabled: hasModelBoundContentProtection(+ appConfig?.filters,+ appConfig?.messageFilter?.pii,+ ),});const context = createResponseContext(request, responseId);diff --git a/client/src/components/Messages/Content/Error/ModelError.tsx b/client/src/components/Messages/Content/Error/ModelError.tsxindex e167b5d600..69575ade13 100644--- a/client/src/components/Messages/Content/Error/ModelError.tsx+++ b/client/src/components/Messages/Content/Error/ModelError.tsx@@ -4,7 +4,13 @@ import {isCodeWorkspaceSelectionErrorReason,} from 'librechat-data-provider';import type { ErrorRendererProps } from './parts';-import { getProviderName, readNumber, readString, useErrorEndpoint } from './parts';+import {+ ErrorWithDetail,+ getProviderName,+ readNumber,+ readString,+ useErrorEndpoint,+} from './parts';import { codeWorkspaceErrorKeys } from '~/utils/errors';import { useLocalize } from '~/hooks';@@ -50,9 +56,21 @@ export default function ModelError({ json, message }: ErrorRendererProps) {: localize('com_error_code_workspace_unavailable');}- /** Provider-neutral, matching the sentence the server persists as the failure's own text. */+ /**+ * Provider-neutral headline, matching the sentence the server persists as the failure's own+ * text. The provider's own message rides along in `message` when the deployment lets provider+ * text through: a gateway or proxy rejection explains itself there, and nothing generic can.+ */const status = readNumber(json, 'status');- return status != null- ? localize('com_error_upstream_model_status', { 0: status })- : localize('com_error_upstream_model');+ const headline =+ status != null+ ? localize('com_error_upstream_model_status', { 0: status })+ : localize('com_error_upstream_model');+ return (+ <ErrorWithDetail+ headline={headline}+ detail={readString(json, 'message')}+ label={localize('com_error_details_provider')}+ />+ );}diff --git a/client/src/components/Messages/Content/Error/ProviderError.tsx b/client/src/components/Messages/Content/Error/ProviderError.tsxindex 08353a7cee..cc42be9782 100644--- a/client/src/components/Messages/Content/Error/ProviderError.tsx+++ b/client/src/components/Messages/Content/Error/ProviderError.tsx@@ -1,6 +1,13 @@import { ErrorTypes, stripLangChainTroubleshootingUrl } from 'librechat-data-provider';import type { ErrorPayload, ErrorRendererProps, UnclassifiedErrorProps } from './parts';-import { ErrorBody, ErrorDetails, readObject, readString, useErrorEndpoint } from './parts';+import {+ ErrorBody,+ ErrorDetails,+ ErrorWithDetail,+ readObject,+ readString,+ useErrorEndpoint,+} from './parts';import { extractJson } from '~/utils/json';import { useLocalize } from '~/hooks';@@ -115,21 +122,11 @@ export function UnclassifiedError({ json, text, message }: UnclassifiedErrorProp: localize('com_error_upstream_model');const headline = prose == null && json != null ? localize('com_error_unknown') : providerHeadline;- if (prose == null) {- return headline;- }-- if (prose.length <= 240 && !/[\r\n]/.test(prose)) {- return (- <ErrorBody>- <div>{headline}</div>- <div className="text-text-secondary">{prose}</div>- </ErrorBody>- );- }-- return withHeadline(headline, {- label: localize('com_error_details_provider'),- value: prose,- });+ return (+ <ErrorWithDetail+ headline={headline}+ detail={prose}+ label={localize('com_error_details_provider')}+ />+ );}diff --git a/client/src/components/Messages/Content/Error/parts.tsx b/client/src/components/Messages/Content/Error/parts.tsxindex 1889161739..2fe1a75cdf 100644--- a/client/src/components/Messages/Content/Error/parts.tsx+++ b/client/src/components/Messages/Content/Error/parts.tsx@@ -299,6 +299,39 @@ export function ErrorDetails({ label, children }: { label: string; children: Rea);}+/** Past a sentence's worth of text, or across lines, a detail is a body to open rather than read. */+const INLINE_DETAIL_LENGTH = 240;++/**+ * A headline plus the failure's own words, the way every provider-produced error reads: what is+ * known first, the reported text second. A single sentence stays in place, where a reader gets it+ * without acting; a body of text collapses under `label`.+ */+export function ErrorWithDetail({+ headline,+ detail,+ label,+}: {+ headline: string;+ detail?: string;+ label: string;+}) {+ if (detail == null) {+ return <>{headline}</>;+ }++ return (+ <ErrorBody>+ <div>{headline}</div>+ {detail.length <= INLINE_DETAIL_LENGTH && !/[\r\n]/.test(detail) ? (+ <div className="text-text-secondary">{detail}</div>+ ) : (+ <ErrorDetails label={label}>{detail}</ErrorDetails>+ )}+ </ErrorBody>+ );+}+export function ErrorActions({ children }: { children: React.ReactNode }) {return <div className="flex flex-wrap items-center gap-2">{children}</div>;}diff --git a/client/src/components/Messages/Content/__tests__/Error.spec.tsx b/client/src/components/Messages/Content/__tests__/Error.spec.tsxindex 0c548c5fb6..944bbc62f8 100644--- a/client/src/components/Messages/Content/__tests__/Error.spec.tsx+++ b/client/src/components/Messages/Content/__tests__/Error.spec.tsx@@ -387,6 +387,59 @@ describe('Error — provider and model identity', () => {expect(document.body.textContent).not.toContain('OpenAI');});+ it.each([+ 'Unexpected token } in JSON',+ 'gateway rejected {request',+ 'invalid "quoted }" value',+ 'path \\ {',+ ])('renders provider punctuation: %s', (explanation) => {+ renderError(+ 'The model provider could not complete this request.\n' ++ JSON.stringify({+ type: ErrorTypes.UPSTREAM_MODEL_ERROR,+ status: 400,+ message: explanation,+ }),+ providerMessage,+ );+ expect(+ screen.getByText(localized('com_error_upstream_model_status', '400')),+ ).toBeInTheDocument();+ expect(screen.getByText(explanation)).toBeInTheDocument();+ expectReadable();+ });++ /** What a gateway or privacy proxy rejects a request with is only stated in its own message. */+ it('reads the provider explanation an upstream failure carries', () => {+ const explanation = 'Request rejected: this prompt cannot be masked safely';+ const { unmount } = renderError(+ { type: ErrorTypes.UPSTREAM_MODEL_ERROR, status: 400, message: explanation },+ providerMessage,+ );++ expect(+ screen.getByText(localized('com_error_upstream_model_status', '400')),+ ).toBeInTheDocument();+ expect(screen.getByText(explanation)).toBeInTheDocument();+ expectReadable();+ unmount();++ const body = `Upstream rejection\n${JSON.stringify({ reason: 'masking_unavailable' })}`.padEnd(+ 400,+ '.',+ );+ renderError(+ { type: ErrorTypes.UPSTREAM_MODEL_ERROR, status: 400, message: body },+ providerMessage,+ );++ const disclosure = screen.getByRole('button', { name: catalog.com_error_details_provider });+ expect(disclosure).toHaveAttribute('aria-expanded', 'false');+ fireEvent.click(disclosure);+ expect(disclosure).toHaveAttribute('aria-expanded', 'true');+ expect(document.body.textContent).toContain(body);+ });+it.each([['required', 'com_error_code_workspace_required'],['invalid', 'com_error_code_workspace_invalid'],diff --git a/client/src/utils/json.spec.ts b/client/src/utils/json.spec.tsnew file mode 100644index 0000000000..8f654591d4--- /dev/null+++ b/client/src/utils/json.spec.ts@@ -0,0 +1,25 @@+import { extractJson } from './json';++describe('extractJson', () => {+ it.each([+ 'Unexpected token }',+ 'missing {request',+ 'escaped "quote" and }',+ 'trailing backslash \\',+ 'backslash before quote \\" }',+ ])('preserves string contents: %s', (message) => {+ const payload = JSON.stringify({ nested: { message }, list: ['{', '}'] });+ expect(extractJson(`prose } before ${payload} after`)).toBe(payload);+ });++ it('returns the first complete object', () => {+ expect(extractJson('prefix {"a":1} {"b":2}')).toBe('{"a":1}');+ });++ it.each(['plain text', '{"message":"unterminated', '{"nested":{}'])(+ 'rejects incomplete objects',+ (text) => {+ expect(extractJson(text)).toBe('');+ },+ );+});diff --git a/client/src/utils/json.ts b/client/src/utils/json.tsindex fb89377ed6..ce2107277d 100644--- a/client/src/utils/json.ts+++ b/client/src/utils/json.ts@@ -1,7 +1,7 @@export function isJson(str: string) {try {JSON.parse(str);- } catch (e) {+ } catch {return false;}return true;@@ -10,7 +10,7 @@ export function isJson(str: string) {export function formatJSON(json: string) {try {return JSON.stringify(JSON.parse(json), null, 2);- } catch (e) {+ } catch {return json;}}@@ -18,14 +18,28 @@ export function formatJSON(json: string) {export function extractJson(text: string) {let openBraces = 0;let startIndex = -1;+ let inString = false;+ let escaped = false;for (let i = 0; i < text.length; i++) {- if (text[i] === '{') {+ if (inString) {+ if (escaped) {+ escaped = false;+ } else if (text[i] === '\\') {+ escaped = true;+ } else if (text[i] === '"') {+ inString = false;+ }+ continue;+ }+ if (text[i] === '"' && openBraces > 0) {+ inString = true;+ } else if (text[i] === '{') {if (openBraces === 0) {startIndex = i;}openBraces++;- } else if (text[i] === '}') {+ } else if (text[i] === '}' && openBraces > 0) {openBraces--;if (openBraces === 0 && startIndex !== -1) {return text.slice(startIndex, i + 1);diff --git a/e2e/specs/mock/completion.spec.ts b/e2e/specs/mock/completion.spec.tsindex b82f0678de..7a172a890a 100644--- a/e2e/specs/mock/completion.spec.ts+++ b/e2e/specs/mock/completion.spec.ts@@ -24,6 +24,7 @@ test.describe('generation finalization invariant', () => {await expect(messagesView(page).getByText('The model provider could not complete this request.'),).toBeVisible();- await expect(messagesView(page).getByText(providerError)).toHaveCount(0);+ /** No content policy is configured here, so the provider's own words reach the reader. */+ await expect(messagesView(page).getByText(providerError)).toBeVisible();});});diff --git a/e2e/specs/mock/message-tree.spec.ts b/e2e/specs/mock/message-tree.spec.tsindex 4c1a955773..1540c1a9b5 100644--- a/e2e/specs/mock/message-tree.spec.ts+++ b/e2e/specs/mock/message-tree.spec.ts@@ -1135,7 +1135,11 @@ test.describe('message tree stream operations', () => {const errorPrompt = `E2E_FORCED_ERROR:${label}`;const providerError = `E2E forced stream error ${label}`;const errorText = 'The model provider could not complete this request.';- const errorPayload = `${errorText}\n${JSON.stringify({ type: 'upstream_model_error' })}`;+ /** No content policy is configured here, so the failure keeps the provider's own words. */+ const errorPayload = `${errorText}\n${JSON.stringify({+ type: 'upstream_model_error',+ message: providerError,+ })}`;const afterErrorPrompt = replyPrompt(`${label}-after-error`);const afterErrorReply = replyText(`${label}-after-error`);@@ -1145,7 +1149,7 @@ test.describe('message tree stream operations', () => {await sendAndExpectReply(page, errorPrompt, errorText);await expect(messagesView(page).getByText(errorText)).toBeVisible({ timeout: 30000 });- await expect(messagesView(page).getByText(providerError)).toHaveCount(0);+ await expect(messagesView(page).getByText(providerError)).toBeVisible();await sendAndExpectReply(page, afterErrorPrompt, afterErrorReply);const messages = await waitForMessages(diff --git a/librechat.example.yaml b/librechat.example.yamlindex 3ab4225018..c36d841be5 100644--- a/librechat.example.yaml+++ b/librechat.example.yaml@@ -567,6 +567,8 @@ endpoints:# agents:# # (optional) Default recursion depth for agents, defaults to 25# recursionLimit: 50+ # # Maximum provider error characters retained without content protection (0 hides detail).+ # maxProviderErrorChars: 2000# # (optional) Max recursion depth for agents, defaults to 25# maxRecursionLimit: 100# # (optional) Abort a run once a single streamed tool call's arguments exceed this many bytes.diff --git a/packages/api/src/agents/errors.spec.ts b/packages/api/src/agents/errors.spec.tsindex a1d9817b4e..868e60ea77 100644--- a/packages/api/src/agents/errors.spec.ts+++ b/packages/api/src/agents/errors.spec.ts@@ -3,6 +3,7 @@ import { GraphRecursionError } from '@langchain/langgraph';import {GENERIC_PROVIDER_ERROR,getLangChainErrorCode,+ getProviderErrorMessage,resolveLangChainError,getUserFacingProviderError,isFatalAgentInitializationError,@@ -128,6 +129,58 @@ describe('LangChain provider error text', () => {expect(getUserFacingProviderError(error, false)).toBe('[object Object]');});});++ describe('getProviderErrorMessage', () => {+ it('reports the provider wording without the docs URL', () => {+ const error = new Error(`400 masking unavailable${troubleshooting('MODEL_NOT_FOUND')}`);+ expect(getProviderErrorMessage(error)).toBe('400 masking unavailable');+ });++ it('strips a troubleshooting suffix crossing the output boundary', () => {+ const explanation = 'x'.repeat(1990);+ const error = new Error(`${explanation}${troubleshooting('INVALID_PROMPT_INPUT')}`);+ expect(getProviderErrorMessage(error)).toBe(explanation);+ });++ it('bounds scanning before stripping a multi-megabyte suffix', () => {+ const explanation = 'x'.repeat(2000);+ const scan = jest.spyOn(String.prototype, 'indexOf');+ expect(getProviderErrorMessage(new Error(explanation + ' '.repeat(2_000_000)))).toBe(+ explanation,+ );+ expect(scan.mock.contexts.every((text) => text.length <= 2256)).toBe(true);+ scan.mockRestore();+ });++ it('bounds an unbounded provider body', () => {+ const error = new Error('x'.repeat(4096));+ expect(getProviderErrorMessage(error)).toBe('x'.repeat(2000));+ });++ it.each([+ ['a rejection thrown as a string', 'proxy refused the request', 'proxy refused the request'],+ ['an error with nothing to say', new Error(' '), undefined],+ [+ 'a non-string message',+ Object.assign(new Error('replaced'), { message: { a: 1 } }),+ undefined,+ ],+ ['a non-object rejection', 42, undefined],+ ])('reads %s defensively', (_case, error, expected) => {+ expect(getProviderErrorMessage(error)).toBe(expected);+ });++ it('contains a hostile message accessor', () => {+ const error = Object.create(null, {+ message: {+ get() {+ throw new Error('hostile message getter');+ },+ },+ });+ expect(getProviderErrorMessage(error)).toBeUndefined();+ });+ });});describe('isStepLimitError', () => {diff --git a/packages/api/src/agents/errors.ts b/packages/api/src/agents/errors.tsindex 81a8ff14b8..c2caff0a7e 100644--- a/packages/api/src/agents/errors.ts+++ b/packages/api/src/agents/errors.ts@@ -1,5 +1,6 @@import {ErrorTypes,+ DEFAULT_MAX_PROVIDER_ERROR_CHARS,parseLangChainErrorCode,stripLangChainTroubleshootingUrl,} from 'librechat-data-provider';@@ -114,6 +115,34 @@ export function getUserFacingProviderError(error: unknown, protectionEnabled: boreturn stripLangChainTroubleshootingUrl(error.message) || GENERIC_PROVIDER_ERROR;}+/** Bounded lookahead covers LangChain's appended troubleshooting label and URL. */+const TROUBLESHOOTING_LOOKAHEAD = 256;++/**+ * The provider's own words for a failure, or `undefined` when it has none to give. A gateway,+ * proxy or OpenAI-compatible endpoint answers a rejection it alone can explain, and that sentence+ * is more specific than any generic string we could write.+ *+ * Read defensively: an SDK error's `message` may be a hostile accessor or a body object rather+ * than a string, and the docs URL LangChain stamps in is not for a reader.+ */+export function getProviderErrorMessage(+ error: unknown,+ maxChars: number = DEFAULT_MAX_PROVIDER_ERROR_CHARS,+): string | undefined {+ const raw =+ error != null && typeof error === 'object' ? readErrorProperty(error, 'message') : error;+ if (typeof raw !== 'string') {+ return undefined;+ }+ const limit =+ Number.isSafeInteger(maxChars) && maxChars >= 0 ? maxChars : DEFAULT_MAX_PROVIDER_ERROR_CHARS;+ const message = stripLangChainTroubleshootingUrl(raw.slice(0, limit + TROUBLESHOOTING_LOOKAHEAD))+ .slice(0, limit)+ .trim();+ return message.length === 0 ? undefined : message;+}+/*** LangGraph's stable machine identifier for "the graph ran out of supersteps".* Set as `lc_error_code` on the `GraphRecursionError` thrown by the Pregel loopdiff --git a/packages/api/src/agents/failures/terminal.spec.ts b/packages/api/src/agents/failures/terminal.spec.tsindex 1ee77bf011..18421f9a3f 100644--- a/packages/api/src/agents/failures/terminal.spec.ts+++ b/packages/api/src/agents/failures/terminal.spec.ts@@ -17,6 +17,7 @@ describe('terminal agent-run error logging', () => {logger,responseMessageId: '78847296-b174-4127-a342-78efa427d4a5',source: '[Agent API]',+ protectionEnabled: true,});observer.modelCallback.handleLLMError(providerError);@@ -44,12 +45,111 @@ describe('terminal agent-run error logging', () => {expect(JSON.stringify(logger.error.mock.calls)).not.toContain('InternalServerException');});+ /** The rejection a gateway or privacy proxy answers with is only stated in its own message. */+ it('carries the provider explanation for an unclassified upstream failure', () => {+ const observer = createTerminalRunErrorObserver({+ logger: { error: jest.fn() },+ source: '[Agent API]',+ protectionEnabled: false,+ });+ const providerError = Object.assign(+ new Error('400 Request rejected: this prompt cannot be masked safely'),+ { status: 400 },+ );+ observer.modelCallback.handleLLMError(providerError);++ expect(+ observer.getUserFacingError(+ new Error('graph failed', { cause: providerError }),+ () => 'fallback',+ ),+ ).toBe(+ 'The model provider could not complete this request.\n' ++ JSON.stringify({+ type: 'upstream_model_error',+ status: 400,+ message: '400 Request rejected: this prompt cannot be masked safely',+ }),+ );+ });++ /** A provider error rethrown without its own text: the terminal error carries the wording. */+ it('falls back to the terminal error wording when the tracked failure has none', () => {+ const observer = createTerminalRunErrorObserver({+ logger: { error: jest.fn() },+ source: '[Agent API]',+ protectionEnabled: false,+ });+ const providerError = Object.assign(new Error(''), { status: 502 });+ observer.modelCallback.handleLLMError(providerError);++ expect(+ observer.getUserFacingError(+ new Error('Bad gateway from proxy', { cause: providerError }),+ () => 'fallback',+ ),+ ).toBe(+ 'The model provider could not complete this request.\n' ++ JSON.stringify({+ type: 'upstream_model_error',+ status: 502,+ message: 'Bad gateway from proxy',+ }),+ );+ });++ it.each([true, undefined])(+ 'withholds provider text when protection is %s',+ (protectionEnabled) => {+ const observer = createTerminalRunErrorObserver({+ logger: { error: jest.fn() },+ source: '[Agent API]',+ protectionEnabled,+ });+ const privateValue = 'PRIVATE-SUBMITTED-CONTENT';+ const providerError = Object.assign(new Error(`400 rejected: ${privateValue}`), {+ status: 400,+ });+ observer.modelCallback.handleLLMError(providerError);++ const userFacingError = observer.getUserFacingError(+ new Error('graph failed', { cause: providerError }),+ () => 'fallback',+ );++ expect(userFacingError).toBe(+ 'The model provider could not complete this request.\n' ++ JSON.stringify({ type: 'upstream_model_error', status: 400 }),+ );+ expect(userFacingError).not.toContain(privateValue);+ },+ );++ it.each([0, 32, 3000])('retains at most the configured %i characters', (limit) => {+ const observer = createTerminalRunErrorObserver({+ logger: { error: jest.fn() },+ source: '[Agent API]',+ protectionEnabled: false,+ maxProviderErrorChars: limit,+ });+ const error = new Error('x'.repeat(4096));+ observer.modelCallback.handleLLMError(error);+ expect(observer.getUserFacingError(error, () => 'fallback')).toBe(+ 'The model provider could not complete this request.\n' ++ JSON.stringify({+ type: 'upstream_model_error',+ ...(limit > 0 ? { message: 'x'.repeat(limit) } : {}),+ }),+ );+ });+it('keeps unrelated terminal failures on the generic safe path', () => {const logger = { error: jest.fn() };const observer = createTerminalRunErrorObserver({logger,responseMessageId: 'response-123',source: '[Agent API]',+ protectionEnabled: false,});observer.modelCallback.handleLLMError(new Error('recovered model attempt'));@@ -64,7 +164,11 @@ describe('terminal agent-run error logging', () => {it('does not log a tracked client cancellation as an upstream failure', () => {const logger = { error: jest.fn() };- const observer = createTerminalRunErrorObserver({ logger, source: '[Agent API]' });+ const observer = createTerminalRunErrorObserver({+ logger,+ source: '[Agent API]',+ protectionEnabled: false,+ });const controller = new AbortController();const abortError = Object.assign(new Error('request aborted'), { name: 'AbortError' });observer.modelCallback.handleLLMError(abortError);@@ -77,7 +181,11 @@ describe('terminal agent-run error logging', () => {it('keeps provider AbortErrors observable while the run signal is live', () => {const logger = { error: jest.fn() };- const observer = createTerminalRunErrorObserver({ logger, source: '[Agent API]' });+ const observer = createTerminalRunErrorObserver({+ logger,+ source: '[Agent API]',+ protectionEnabled: false,+ });const abortError = Object.assign(new Error('provider aborted'), { name: 'AbortError' });observer.modelCallback.handleLLMError(abortError);@@ -91,7 +199,11 @@ describe('terminal agent-run error logging', () => {it('keeps real provider failures observable when Stop wins the same-tick race', () => {const logger = { error: jest.fn() };- const observer = createTerminalRunErrorObserver({ logger, source: '[Agent API]' });+ const observer = createTerminalRunErrorObserver({+ logger,+ source: '[Agent API]',+ protectionEnabled: false,+ });const controller = new AbortController();const providerError = new Error('provider failed');observer.modelCallback.handleLLMError(providerError);@@ -109,6 +221,7 @@ describe('terminal agent-run error logging', () => {const observer = createTerminalRunErrorObserver({logger: { error: jest.fn() },source: '[Agent API]',+ protectionEnabled: false,});const providerError = new Error('provider failed');const terminalError = Object.assign(new Error('rate limited', { cause: providerError }), {@@ -125,6 +238,7 @@ describe('terminal agent-run error logging', () => {const observer = createTerminalRunErrorObserver({logger: { error: jest.fn() },source: '[Agent API]',+ protectionEnabled: false,});const providerError = Object.create(null, {lc_error_code: {diff --git a/packages/api/src/agents/failures/terminal.ts b/packages/api/src/agents/failures/terminal.tsindex c03c993fef..ce2fcf5909 100644--- a/packages/api/src/agents/failures/terminal.ts+++ b/packages/api/src/agents/failures/terminal.ts@@ -2,9 +2,9 @@ import { ErrorTypes } from 'librechat-data-provider';import type { SafeErrorMetadata } from '../../utils/errors';import type { ModelErrorTrackerCallback } from './tracker';import { getSafeErrorMetadata, isOwnedAbortError } from '../../utils/errors';+import { getProviderErrorMessage, resolveLangChainError } from '../errors';import { traceIdForMessage } from '../../langfuse/trace';import { createModelErrorTracker } from './tracker';-import { resolveLangChainError } from '../errors';const UPSTREAM_MODEL_ERROR_CODE = 'UPSTREAM_MODEL_ERROR';const UPSTREAM_MODEL_ERROR_ORIGIN = 'model_provider';@@ -63,11 +63,21 @@ export function createTerminalRunErrorObserver({logger,responseMessageId,source,+ protectionEnabled,+ maxProviderErrorChars,genericMessage = `${source} Error:`,}: {logger: TerminalRunErrorLogger;responseMessageId?: string;source: string;+ /**+ * Whether a content policy inspects this deployment's traffic. A provider error body may echo+ * submitted content, so its text stays out of the failure a reader sees while one is active —+ * the same condition every other user-facing failure text is decided by. Omission fails closed+ * for JavaScript callers and older integrations.+ */+ protectionEnabled?: boolean;+ maxProviderErrorChars?: number;genericMessage?: string;}): TerminalRunErrorObserver {const modelErrorTracker = createModelErrorTracker();@@ -86,9 +96,18 @@ export function createTerminalRunErrorObserver({}const { status } = getSafeErrorMetadata(upstreamModelError);+ /** Unclassified: the provider's own explanation is the only account of what happened, and a+ * rejection from a gateway or proxy carries it as the whole point of the 400. The status+ * headlines it either way, so a deployment withholding provider text loses no taxonomy. */+ const providerMessage =+ protectionEnabled !== false+ ? undefined+ : (getProviderErrorMessage(upstreamModelError, maxProviderErrorChars) ??+ getProviderErrorMessage(error, maxProviderErrorChars));return `${UPSTREAM_MODEL_ERROR_FALLBACK}\n${JSON.stringify({type: ErrorTypes.UPSTREAM_MODEL_ERROR,...(status != null ? { status } : {}),+ ...(providerMessage != null ? { message: providerMessage } : {}),})}`;},log(error: unknown, signal?: AbortSignal) {diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.tsindex 3ad4c89ff5..f8a3a730ab 100644--- a/packages/data-provider/src/config.ts+++ b/packages/data-provider/src/config.ts@@ -1211,11 +1211,20 @@ export type CodeWorkerEnrollmentPolicy = NonNullable<NonNullable<z.infer<typeof agentsEndpointSchema>['statefulCodeSessions']>['principalWorkers']>;+export const DEFAULT_MAX_PROVIDER_ERROR_CHARS = 2000;+export const agentsEndpointSchema = baseEndpointSchema.omit({ baseURL: true }).merge(z.object({/* agents specific */+ /** Maximum provider error characters retained in unprotected terminal failures. */+ maxProviderErrorChars: z+ .number()+ .int()+ .min(0)+ .max(1_000_000)+ .default(DEFAULT_MAX_PROVIDER_ERROR_CHARS),recursionLimit: z.number().optional(),disableBuilder: z.boolean().optional().default(false),/** Optional workspace guidance acquisition budget, separate from command execution. */diff --git a/packages/data-provider/src/provider.spec.ts b/packages/data-provider/src/provider.spec.tsnew file mode 100644index 0000000000..a797dd1754--- /dev/null+++ b/packages/data-provider/src/provider.spec.ts@@ -0,0 +1,23 @@+import { configSchema, DEFAULT_MAX_PROVIDER_ERROR_CHARS } from './config';++describe('provider error retention configuration', () => {+ it.each([0, 32, 3000])('preserves a configured limit of %i', (maxProviderErrorChars) => {+ const config = configSchema.parse({+ version: '1.3.1',+ endpoints: { agents: { maxProviderErrorChars } },+ });+ expect(config.endpoints?.agents?.maxProviderErrorChars).toBe(maxProviderErrorChars);+ });++ it('defaults to 2000 characters', () => {+ const config = configSchema.parse({ version: '1.3.1', endpoints: { agents: {} } });+ expect(config.endpoints?.agents?.maxProviderErrorChars).toBe(DEFAULT_MAX_PROVIDER_ERROR_CHARS);+ });++ it.each([-1, 1.5, Infinity, 1_000_001])('rejects invalid limit %s', (maxProviderErrorChars) => {+ expect(+ configSchema.safeParse({ version: '1.3.1', endpoints: { agents: { maxProviderErrorChars } } })+ .success,+ ).toBe(false);+ });+});