Skip to content

Commit de1b410

Browse files
authored
fix(llm): satisfy lowercase json_object prompt checks (#887)
* fix(llm): satisfy lowercase json_object prompt checks * style(llm): preserve JSON acronym in prompt
1 parent 73453f8 commit de1b410

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/llm/backends/openai.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ def _json_object_instruction(response_format: type[BaseModel]) -> str:
3030
instruction — the deriver issues one structured call per batch on the worker
3131
hot path and would otherwise re-walk the schema + re-serialize it every call.
3232
"""
33-
# "JSON" must appear in the messages to satisfy the json_object contract.
33+
# Some OpenAI-compatible providers enforce this JSON-object precondition with
34+
# a case-sensitive substring check, so include lowercase "json" explicitly.
3435
return (
35-
"You must respond with a single JSON object that conforms exactly to "
36-
"the following JSON schema. Do not include any text, markdown, or code "
37-
"fences outside the JSON object.\n\nJSON schema:\n"
36+
"You must respond with a single JSON object (json) that conforms "
37+
"exactly to the following JSON schema. Do not include any text, "
38+
"markdown, or code fences outside the JSON object.\n\nJSON schema:\n"
3839
f"{json.dumps(response_format.model_json_schema())}"
3940
)
4041

tests/llm/test_backends/test_openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ async def test_structured_output_json_object_mode_request_shape() -> None:
808808
assert system_messages, "expected a system message carrying the schema"
809809
system_content = system_messages[0]["content"]
810810
assert "JSON" in system_content
811+
assert "json" in system_content
811812
assert "answer" in system_content # schema property serialized in
812813
assert isinstance(result.content, _StructuredResponse)
813814
assert result.content.answer == "ok"
@@ -943,4 +944,6 @@ async def test_stream_structured_output_json_object_mode() -> None:
943944
call = _await_kwargs(client.chat.completions.create)
944945
assert call["response_format"] == {"type": "json_object"}
945946
system_messages = [m for m in call["messages"] if m["role"] == "system"]
946-
assert system_messages and "JSON" in system_messages[0]["content"]
947+
assert system_messages
948+
assert "JSON" in system_messages[0]["content"]
949+
assert "json" in system_messages[0]["content"]

0 commit comments

Comments
 (0)