Add option to return malformed JSON string from extractJsonFromData (#5578) * feat: add option to return malformed JSON string from extractJsonFromData Fixes #5569 * fix: add fallback for Perplexity case

e5ae782705ee392bede099d3155931bde552d6b1

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +16 -4Ignore whitespace
public/script.js+15 -4
@@ -3929,7 +3929,7 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
39293929 * @prop {number} [responseLength] Maximum response length. If unset, the global default value is used.
39303930 * @prop {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.
39313931 * @prop {string} [prefill] An optional prefill for the prompt.
39323932 * @prop {objectJsonSchema} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction.
39333933 */
39343934
39353935/**
@@ -4041,7 +4041,7 @@ export async function generateRawData({ prompt = '', api = null, instructOverrid
40414041 }
40424042
40434043 if (jsonSchema) {
40444044 return extractJsonFromData(data, { mainApi: api, returnInvalidJson: jsonSchema.returnInvalid });
40454045 }
40464046
40474047 return data;
@@ -4204,6 +4204,7 @@ function removeLastMessage() {
42044204 * @property {object} value JSON schema value.
42054205 * @property {string} [description] Description of the schema.
42064206 * @property {boolean} [strict] If true, the schema will be used in strict mode, meaning that only the fields defined in the schema will be allowed.
4207+ * @property {boolean} [returnInvalid] If true, a string that can't be parsed as a JSON will be returned as is, instead of an empty object.
42074208 *
42084209 * @typedef {object} GenerateOptions
42094210 * @property {boolean} [automatic_trigger] If the generation was triggered automatically (e.g. group auto mode).
@@ -5419,7 +5420,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
54195420
54205421 if (jsonSchema) {
54215422 unblockGeneration(type);
5422- return extractJsonFromData(data);
5423+ return extractJsonFromData(data, { returnInvalidJson: jsonSchema.returnInvalid ?? false });
54235424 }
54245425
54255426 //const getData = await response.json();
@@ -6242,9 +6243,13 @@ export function extractMessageFromData(data, activeApi = null) {
62426243/**
62436244 * Extracts JSON from the response data.
62446245 * @param {object} data Response data
6246+ * @param {object} [options] Extraction options
6247+ * @param {string} [options.mainApi] Main API to use
6248+ * @param {string} [options.chatCompletionSource] Chat completion source
6249+ * @param {boolean} [options.returnInvalidJson=false] Whether to return the raw JSON string even if it fails to parse
62456250 * @returns {string} Extracted JSON string from the response data
62466251 */
62476252export function extractJsonFromData(data, { mainApi = null, chatCompletionSource = null, returnInvalidJson = false } = {}) {
62486253 mainApi = mainApi ?? main_api;
62496254 chatCompletionSource = chatCompletionSource ?? oai_settings.chat_completion_source;
62506255
@@ -6267,6 +6272,9 @@ export function extractJsonFromData(data, { mainApi = null, chatCompletionSource
62676272 break;
62686273 case chat_completion_sources.PERPLEXITY:
62696274 result = tryParse(removeReasoningFromString(text));
6275+ if (!result && returnInvalidJson) {
6276+ return text;
6277+ }
62706278 break;
62716279 case chat_completion_sources.VERTEXAI:
62726280 case chat_completion_sources.MAKERSUITE:
@@ -6287,6 +6295,9 @@ export function extractJsonFromData(data, { mainApi = null, chatCompletionSource
62876295 case chat_completion_sources.ZAI:
62886296 default:
62896297 result = tryParse(text);
6298+ if (!result && returnInvalidJson) {
6299+ return text;
6300+ }
62906301 break;
62916302 }
62926303 } break;
public/scripts/custom-request.js+1 -0
@@ -51,6 +51,7 @@ import EventSourceStream from './sse-stream.js';
5151 * @property {string} [reverse_proxy] - Optional reverse proxy URL
5252 * @property {string} [proxy_password] - Optional proxy password
5353 * @property {string} [custom_prompt_post_processing] - Optional custom prompt post-processing
54+ * @property {import('../script.js').JsonSchema} [json_schema] - Optional JSON schema for structured generation
5455 */
5556
5657/** @typedef {Record<string, any> & ChatCompletionPayloadBase} ChatCompletionPayload */