fix: Filter out null chars from OpenAI response (#5187)

This commit is contained in:
Michael Genson
2025-03-07 10:34:32 -06:00
committed by GitHub
parent 98472ff471
commit 9a469fe4fd
2 changed files with 75 additions and 6 deletions

View File

@@ -1,7 +1,12 @@
import re
from typing import Self
from pydantic import BaseModel
from mealie.core.root_logger import get_logger
RE_NULLS = re.compile(r"[\x00\u0000]|\\u0000")
logger = get_logger()
@@ -14,14 +19,26 @@ class OpenAIBase(BaseModel):
__doc__ = "" # we don't want to include the docstring in the JSON schema
@classmethod
def parse_openai_response(cls, response: str | None):
"""
This method should be implemented in the child class. It should
parse the JSON response from OpenAI and return a dictionary.
"""
def _preprocess_response(cls, response: str | None) -> str:
if not response:
return ""
response = re.sub(RE_NULLS, "", response)
return response
@classmethod
def _process_response(cls, response: str) -> Self:
try:
return cls.model_validate_json(response or "")
return cls.model_validate_json(response)
except Exception:
logger.debug(f"Failed to parse OpenAI response as {cls}. Response: {response}")
raise
@classmethod
def parse_openai_response(cls, response: str | None) -> Self:
"""
Parse the OpenAI response into a class instance.
"""
response = cls._preprocess_response(response)
return cls._process_response(response)