Skip to content

Commit 18ec7c2

Browse files
committed
Add support for structured output in gemini + anthropic
1 parent 776a749 commit 18ec7c2

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

llms/extensions/providers/anthropic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ async def chat(self, chat, context=None):
165165
if "tool_choice" in chat:
166166
anthropic_request["tool_choice"] = chat["tool_choice"]
167167

168+
if "response_format" in chat:
169+
response_format = chat["response_format"]
170+
if response_format.get("type") == "json_schema":
171+
json_schema = response_format.get("json_schema", {})
172+
if "schema" in json_schema:
173+
anthropic_request["output_config"] = {
174+
"format": {
175+
"type": "json_schema",
176+
"schema": json_schema["schema"],
177+
}
178+
}
179+
168180
ctx.log(f"POST {self.chat_url}")
169181
ctx.log(json.dumps(anthropic_request, indent=2))
170182

llms/extensions/providers/google.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ async def chat(self, chat, context=None):
327327
elif self.thinking_config:
328328
generation_config["thinkingConfig"] = self.thinking_config
329329

330+
if "response_format" in chat:
331+
response_format = chat["response_format"]
332+
if isinstance(response_format, dict):
333+
if response_format.get("type") == "json_object":
334+
generation_config["responseMimeType"] = "application/json"
335+
elif response_format.get("type") == "json_schema":
336+
json_schema = response_format.get("json_schema", {})
337+
if "schema" in json_schema:
338+
generation_config["responseMimeType"] = "application/json"
339+
generation_config["responseJsonSchema"] = sanitize_parameters(json_schema["schema"])
340+
330341
if len(generation_config) > 0:
331342
gemini_chat["generationConfig"] = generation_config
332343

0 commit comments

Comments
 (0)