mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-20 04:23:01 +03:00
Fix GenAI structured response not handling optional fields correctly
This commit is contained in:
@@ -187,16 +187,35 @@ def build_review_description_response_format(concerns: list[str]) -> dict[str, A
|
|||||||
|
|
||||||
Strips the `time` field (populated server-side) and drops
|
Strips the `time` field (populated server-side) and drops
|
||||||
`other_concerns` when no concerns are configured.
|
`other_concerns` when no concerns are configured.
|
||||||
|
|
||||||
|
Every remaining property must be listed in `required` because strict
|
||||||
|
providers (OpenAI) reject a schema with any optional property.
|
||||||
"""
|
"""
|
||||||
schema = ReviewMetadata.model_json_schema()
|
schema = ReviewMetadata.model_json_schema()
|
||||||
schema.get("properties", {}).pop("time", None)
|
properties = schema.setdefault("properties", {})
|
||||||
|
required = schema.setdefault("required", [])
|
||||||
|
properties.pop("time", None)
|
||||||
|
|
||||||
if "time" in schema.get("required", []):
|
if "time" in required:
|
||||||
schema["required"].remove("time")
|
required.remove("time")
|
||||||
if not concerns:
|
|
||||||
schema.get("properties", {}).pop("other_concerns", None)
|
if concerns:
|
||||||
if "other_concerns" in schema.get("required", []):
|
# The model field is nullable with a default so stored metadata
|
||||||
schema["required"].remove("other_concerns")
|
# without concerns still parses. For generation, require a plain list
|
||||||
|
# instead; the model returns an empty list when no concern applies.
|
||||||
|
properties["other_concerns"] = {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"type": "string"},
|
||||||
|
"description": properties["other_concerns"]["description"],
|
||||||
|
}
|
||||||
|
|
||||||
|
if "other_concerns" not in required:
|
||||||
|
required.append("other_concerns")
|
||||||
|
else:
|
||||||
|
properties.pop("other_concerns", None)
|
||||||
|
|
||||||
|
if "other_concerns" in required:
|
||||||
|
required.remove("other_concerns")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "json_schema",
|
"type": "json_schema",
|
||||||
|
|||||||
@@ -7,11 +7,47 @@ from frigate.genai.prompts import (
|
|||||||
REVIEW_DESCRIPTION_FIELD_GUIDELINES,
|
REVIEW_DESCRIPTION_FIELD_GUIDELINES,
|
||||||
REVIEW_RESPONSE_STYLES,
|
REVIEW_RESPONSE_STYLES,
|
||||||
build_review_description_prompt,
|
build_review_description_prompt,
|
||||||
|
build_review_description_response_format,
|
||||||
build_review_summary_prompt,
|
build_review_summary_prompt,
|
||||||
get_review_field_guidelines,
|
get_review_field_guidelines,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestReviewDescriptionResponseFormat(unittest.TestCase):
|
||||||
|
def _schema(self, concerns: list[str]) -> dict:
|
||||||
|
response_format = build_review_description_response_format(concerns)
|
||||||
|
self.assertTrue(response_format["json_schema"]["strict"])
|
||||||
|
return response_format["json_schema"]["schema"]
|
||||||
|
|
||||||
|
def test_every_property_is_required_for_strict_mode(self):
|
||||||
|
for concerns in ([], ["a person loitering near the gate"]):
|
||||||
|
with self.subTest(concerns=concerns):
|
||||||
|
schema = self._schema(concerns)
|
||||||
|
self.assertEqual(set(schema["properties"]), set(schema["required"]))
|
||||||
|
|
||||||
|
def test_time_is_never_requested(self):
|
||||||
|
schema = self._schema(["a person loitering near the gate"])
|
||||||
|
self.assertNotIn("time", schema["properties"])
|
||||||
|
self.assertNotIn("time", schema["required"])
|
||||||
|
|
||||||
|
def test_concerns_omitted_when_not_configured(self):
|
||||||
|
schema = self._schema([])
|
||||||
|
self.assertNotIn("other_concerns", schema["properties"])
|
||||||
|
self.assertNotIn("other_concerns", schema["required"])
|
||||||
|
|
||||||
|
def test_concerns_requested_as_plain_list_when_configured(self):
|
||||||
|
schema = self._schema(["a person loitering near the gate"])
|
||||||
|
concerns = schema["properties"]["other_concerns"]
|
||||||
|
self.assertEqual(concerns["type"], "array")
|
||||||
|
self.assertEqual(concerns["items"], {"type": "string"})
|
||||||
|
self.assertNotIn("anyOf", concerns)
|
||||||
|
self.assertNotIn("default", concerns)
|
||||||
|
|
||||||
|
def test_schema_is_not_shared_between_calls(self):
|
||||||
|
self._schema(["a person loitering near the gate"])
|
||||||
|
self.assertNotIn("other_concerns", self._schema([])["properties"])
|
||||||
|
|
||||||
|
|
||||||
class TestReviewResponseStyle(unittest.TestCase):
|
class TestReviewResponseStyle(unittest.TestCase):
|
||||||
def _build_prompt(self, response_style: str = "default") -> str:
|
def _build_prompt(self, response_style: str = "default") -> str:
|
||||||
review_data = {
|
review_data = {
|
||||||
|
|||||||
Reference in New Issue
Block a user