From 87abfd801ed2cdea856a0240942130be35198415 Mon Sep 17 00:00:00 2001 From: KoalaTrapLord <265133236+KoalaTrapLord@users.noreply.github.com> Date: Thu, 19 Mar 2026 10:04:36 +0000 Subject: [PATCH] Fix V3 500 error: replace json_schema response_format with json_object - Sonar does not support response_format type json_schema - Switch to {"type": "json_object"} which Sonar supports - Keep json_schema/schema_name params for call-site compatibility (ignored) - Add exc_info=True to V3 generate error logging for full tracebacks --- coach/clients/perplexity_client.py | 27 +++++++++++++-------------- routes/deckgen.py | 4 +--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/coach/clients/perplexity_client.py b/coach/clients/perplexity_client.py index f651e34..3164616 100644 --- a/coach/clients/perplexity_client.py +++ b/coach/clients/perplexity_client.py @@ -72,26 +72,31 @@ class PerplexityClient: self, system_prompt: str, user_prompt: str, - json_schema: dict, + json_schema: dict = None, schema_name: str = "response", model: str = None, temperature: float = 0.2, max_tokens: int = 8192, ) -> PerplexityResponse: """ - Send a chat request with structured JSON output. + Send a chat request with JSON output. + + Uses response_format={"type": "json_object"} because Perplexity's + Sonar models do not support the json_schema response_format. + Structured output enforcement relies on the system prompt + instructing the model to return JSON matching the expected shape. Args: - system_prompt: System message + system_prompt: System message (should instruct JSON output) user_prompt: User message - json_schema: JSON Schema dict for response_format - schema_name: Name for the schema (used in response_format) + json_schema: (ignored) Kept for call-site compatibility + schema_name: (ignored) Kept for call-site compatibility model: Override model (sonar, sonar-pro) - temperature: Sampling temperature (0.0–1.0) + temperature: Sampling temperature (0.0-1.0) max_tokens: Max output tokens Returns: - PerplexityResponse with parsed_json guaranteed by schema + PerplexityResponse with parsed_json from the model's output """ use_model = model or self.model @@ -106,13 +111,7 @@ class PerplexityClient: messages=messages, temperature=temperature, max_tokens=max_tokens, - response_format={ - "type": "json_schema", - "json_schema": { - "name": schema_name, - "schema": json_schema, - }, - }, + response_format={"type": "json_object"}, ) choice = response.choices[0] diff --git a/routes/deckgen.py b/routes/deckgen.py index c1269b4..3f5dedf 100644 --- a/routes/deckgen.py +++ b/routes/deckgen.py @@ -1290,9 +1290,7 @@ async def deck_gen_v3_generate(req: DeckGenV3Request): except ValueError as e: return JSONResponse({'error': str(e)}, status_code=422) except Exception as e: - log_deckgen.error(f'Error: {e}') - import traceback - traceback.print_exc() + log_deckgen.error(f'V3 generate error: {e}', exc_info=True) raise HTTPException(500, f'Deck generation failed: {str(e)}')