mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-28 05:05:12 -05:00
fix: Filter out null chars from OpenAI response (#5187)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user