Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,56 @@ describe('matchUnsupportedParameter', () => {
test('returns undefined for an empty body', () => {
expect(matchUnsupportedParameter('')).toBeUndefined();
});

// Structured unknown-field errors (e.g. Alibaba Cloud DashScope) name the
// offending field in a machine-readable `data.field` member with a
// localized message and a `code` of UNKNOWN_FIELD, instead of the English
// "Unsupported parameter: X" text. See issue #783.
test('extracts the field from a structured UNKNOWN_FIELD body (#783)', () => {
const body = JSON.stringify({
code: 'UNKNOWN_FIELD',
message: '未知请求字段:reasoning.effort',
data: { field: 'reasoning.effort' },
traceId: 'trace_92b98af4-b0f9-4742-b50b-31f3ac25e824',
});
expect(matchUnsupportedParameter(body)).toBe('reasoning.effort');
});

test('extracts the field from a structured UNKNOWN_FIELD body with a dotted path', () => {
expect(
matchUnsupportedParameter(
'{"code":"UNKNOWN_FIELD","message":"unknown field","data":{"field":"text.verbosity"}}'
)
).toBe('text.verbosity');
});

test('normalizes bracket notation in a structured UNKNOWN_FIELD field', () => {
expect(
matchUnsupportedParameter('{"code":"UNKNOWN_FIELD","data":{"field":"messages[0].name"}}')
).toBe('messages.0.name');
});

test('accepts data.field_path as an alternative key', () => {
expect(
matchUnsupportedParameter('{"code":"UNKNOWN_FIELD","data":{"field_path":"reasoning.effort"}}')
).toBe('reasoning.effort');
});

test('returns undefined for a structured body without the unknown-field code', () => {
expect(
matchUnsupportedParameter('{"code":"RATE_LIMITED","data":{"field":"reasoning.effort"}}')
).toBeUndefined();
});

test('returns undefined for a structured body without a data.field', () => {
expect(
matchUnsupportedParameter('{"code":"UNKNOWN_FIELD","message":"bad request"}')
).toBeUndefined();
});

test('returns undefined for a non-JSON body', () => {
expect(matchUnsupportedParameter('not json at all')).toBeUndefined();
});
});

describe('deleteDottedPath', () => {
Expand Down Expand Up @@ -598,6 +648,18 @@ describe('planUnsupportedParamStrip', () => {
expect(state.attempts).toBe(0);
});

test('strips a field named by a structured UNKNOWN_FIELD body (#783)', () => {
const state = createUnsupportedParamStripState();
const body = JSON.stringify({
code: 'UNKNOWN_FIELD',
message: '未知请求字段:reasoning.effort',
data: { field: 'reasoning.effort' },
});
expect(planUnsupportedParamStrip(body, state)).toBe('reasoning.effort');
expect(state.attempts).toBe(1);
expect(state.strippedParams.has('reasoning.effort')).toBe(true);
});

test('structural guard: refuses to strip the whole messages/input/model field, consuming no budget', () => {
// Deleting any of these wholesale guarantees a malformed request — every
// retry would 400 on a missing-conversation/missing-model error instead,
Expand Down
43 changes: 43 additions & 0 deletions packages/backend/src/services/dispatch/dispatcher-auto-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,53 @@ function normalizeBracketSegments(path: string): string {
*/
export function matchUnsupportedParameter(responseBody: string): string | undefined {
if (!responseBody) return undefined;

// Structured error formats that name an unsupported/unknown field in a
// machine-readable location rather than a free-text "Unsupported
// parameter: X" message. Example (Alibaba Cloud DashScope):
// {"code":"UNKNOWN_FIELD","message":"未知请求字段:reasoning.effort","data":{"field":"reasoning.effort"}}
// Without this, a provider returning a localized/structured unknown-field
// error bypasses the strip-and-retry loop and the request fails outright.
const structured = matchStructuredUnknownField(responseBody);
if (structured) return structured;

const paramName = UNSUPPORTED_PARAMETER_PATTERN.exec(responseBody)?.[1];
return paramName === undefined ? undefined : normalizeBracketSegments(paramName);
}

/**
* Matches structured upstream error bodies that report an unknown/unrecognized
* request field via a `data.field` (or `data.field_path`) member rather than an
* English "Unsupported parameter" message. Covers Alibaba Cloud DashScope
* ({"code":"UNKNOWN_FIELD",...,"data":{"field":"reasoning.effort"}}),
* which is the shape reported in #783.
*
* Returns the canonical (dotted) field path, or `undefined` when the body is
* not one of these structured shapes.
*/
function matchStructuredUnknownField(responseBody: string): string | undefined {
let parsed: any;
try {
parsed = JSON.parse(responseBody);
} catch {
return undefined;
}
if (!parsed || typeof parsed !== 'object') return undefined;

// DashScope-style: { "code": "UNKNOWN_FIELD", "data": { "field": "..." } }
const code = typeof parsed.code === 'string' ? parsed.code.toUpperCase() : undefined;
const isUnknownField =
code === 'UNKNOWN_FIELD' || code === 'INVALID_PARAMETER' || code === 'UNEXPECTED_PARAMETER';
if (!isUnknownField) return undefined;

const data = parsed.data;
if (!data || typeof data !== 'object') return undefined;
const raw = data.field ?? data.field_path ?? data.fieldPath;
if (typeof raw !== 'string' || !raw) return undefined;

return normalizeBracketSegments(raw);
}

/** Segment names that must never be traversed — see `deleteDottedPath`. */
const DANGEROUS_PATH_SEGMENTS = new Set(['__proto__', 'constructor', 'prototype']);

Expand Down