TencentCloud/TencentDB-Agent-Memory · error
invalid model_id
Error message
invalid model_id
What it means
validateDimension guards the per-model rate-limit lookup endpoints: it asserts instance_id is a safe key segment and rejects model_id that is empty, longer than 256 chars, or contains control characters (U+0000–U+001F). The thrown Error('invalid model_id') is a request-validation failure, not an upstream error.
Source
Thrown at MemoryProxy/src/routes/rate-limits.ts:135
function readDimension(
body: RateLimitBody,
): { instanceId: string; modelId: string } | Error | null {
const instanceId = typeof body.instance_id === "string" ? body.instance_id.trim() : "";
const modelId = typeof body.model_id === "string" ? body.model_id.trim() : "";
if (!instanceId && !modelId) return null;
if (!instanceId || !modelId) return new Error("instance_id and model_id must be provided together");
try {
validateDimension(instanceId, modelId);
return { instanceId, modelId };
} catch (err) {
return err instanceof Error ? err : new Error(String(err));
}
}
function validateDimension(instanceId: string, modelId: string): void {
assertKeySegment("instance_id", instanceId);
if (!modelId || modelId.length > 256 || /[\u0000-\u001f]/.test(modelId)) {
throw new Error("invalid model_id");
}
}
function positiveInteger(value: unknown): number | null {
return typeof value === "number" && Number.isSafeInteger(value) && value > 0
? value
: null;
}
function ok(c: Context, data: Record<string, unknown>): Response {
return c.json({ code: 0, message: "ok", data });
}
function error(c: Context, status: 400 | 503, message: string): Response {
return c.json({ code: status, message }, status);
}
View on GitHub (pinned to 3efcd317b8)
Solutions
- Ensure the client passes the actual model name (e.g. 'gpt-4o') in the path segment
- Trim/sanitize modelId on the caller side and reject empty values before the request
- URL-encode the model id and strip control characters/whitespace before building the URL
- Check for template placeholder bugs like '${modelId}' not being substituted
Example fix
// before
const url = `/rate-limits/${instanceId}/${rawModelId}`;
// after
const modelId = String(rawModelId).trim();
if (!modelId || modelId.length > 256 || /[\u0000-\u001f]/.test(modelId)) throw new Error("invalid model_id");
const url = `/rate-limits/${encodeURIComponent(instanceId)}/${encodeURIComponent(modelId)}`; Defensive patterns
Strategy: validation
Validate before calling
function validModelId(modelId) {
return typeof modelId === "string" && modelId.length > 0 && modelId.length <= 256 && !/[\u0000-\u001f]/.test(modelId);
}
if (!validModelId(modelId)) throw new Error("invalid model_id"); Type guard
function isValidModelId(v: unknown): v is string {
return typeof v === "string" && v.length > 0 && v.length <= 256 && !/[\u0000-\u001f]/.test(v);
} Try / catch
try {
await getRateLimits(instanceId, modelId);
} catch (e) {
if (e.message === "invalid model_id") return respond400("model_id must be a non-empty string without control characters");
throw e;
} Prevention
- Sanitize and encodeURIComponent path segments before building URLs
- Reject empty/template-placeholder values like '${modelId}' at the client
- Cap segment length to 256 and strip control characters
- Add request-shape tests covering junk path segments
When it happens
Trigger: GET /rate-limits/:instance_id/:model_id (handleGet) or readDimension with a model_id that is empty, >256 chars, or contains control characters — typically from URL-encoded junk, path traversal attempts, or a client passing a whole config blob as the model id.
Common situations: Client template variables left unsubstituted in the URL; log lines or multi-line strings accidentally interpolated into the path; malicious probing with control characters.
Related errors
- memory 系统用户 key 必须匹配 sk-mem-[A-Za-z0-9_-]{32}
- llm.provider=proxy 且 useMemorySystemUserKey=false 时必须显式 llm.
- Generation log object key exceeds COS limit
- Invalid generation log key
- Invalid generation log cursor
AI-assisted analysis of TencentCloud/TencentDB-Agent-Memory@3efcd317b8 (2026-09-01).
Data as JSON: /api/errors/3a6eab981bdffd97.
Report an issue: GitHub.