From d9b2cadcbb080cd6b3733084b8ea3454f5c2ab4b Mon Sep 17 00:00:00 2001 From: Alexandre Menasria Date: Wed, 26 Mar 2025 10:29:11 +0100 Subject: [PATCH] Add examples for using raw json schema without pydantic --- .../structured_outputs_with_json_schema.py | 117 ++++++++++++++++++ ...py => structured_outputs_with_pydantic.py} | 0 2 files changed, 117 insertions(+) create mode 100644 examples/structured_outputs_with_json_schema.py rename examples/{structured_outputs.py => structured_outputs_with_pydantic.py} (100%) diff --git a/examples/structured_outputs_with_json_schema.py b/examples/structured_outputs_with_json_schema.py new file mode 100644 index 00000000..69ac9690 --- /dev/null +++ b/examples/structured_outputs_with_json_schema.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python + +import os + +from mistralai import Mistral + + +def main(): + api_key = os.environ["MISTRAL_API_KEY"] + client = Mistral(api_key=api_key) + + print("Using the .complete method to input a raw json schema to the API:\n") + # When providing raw JSON Schema to the SDK you need to have 'additionalProperties': False in the schema definition + # This is because the API is only accepting a strict JSON Schema + chat_response = client.chat.complete( + model="mistral-large-latest", + messages=[ + { + "role": "system", + "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.", + }, + {"role": "user", "content": "How can I solve 8x + 7 = -23"}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "MathDemonstration", + "schema_definition": { + "$defs": { + "Explanation": { + "properties": { + "explanation": { + "title": "Explanation", + "type": "string", + }, + "output": {"title": "Output", "type": "string"}, + }, + "required": ["explanation", "output"], + "title": "Explanation", + "type": "object", + "additionalProperties": False, + } + }, + "properties": { + "steps": { + "items": {"$ref": "#/$defs/Explanation"}, + "title": "Steps", + "type": "array", + }, + "final_answer": {"title": "Final Answer", "type": "string"}, + }, + "required": ["steps", "final_answer"], + "title": "MathDemonstration", + "type": "object", + "additionalProperties": False, + }, + "description": None, + "strict": True, + }, + }, + ) + print(chat_response.choices[0].message.content) + + # Or with the streaming API + with client.chat.stream( + model="mistral-large-latest", + messages=[ + { + "role": "system", + "content": "You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer. For each step, just provide the output as an equation use the explanation field to detail the reasoning.", + }, + {"role": "user", "content": "How can I solve 8x + 7 = -23"}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "MathDemonstration", + "schema_definition": { + "$defs": { + "Explanation": { + "properties": { + "explanation": { + "title": "Explanation", + "type": "string", + }, + "output": {"title": "Output", "type": "string"}, + }, + "required": ["explanation", "output"], + "title": "Explanation", + "type": "object", + "additionalProperties": False, + } + }, + "properties": { + "steps": { + "items": {"$ref": "#/$defs/Explanation"}, + "title": "Steps", + "type": "array", + }, + "final_answer": {"title": "Final Answer", "type": "string"}, + }, + "required": ["steps", "final_answer"], + "title": "MathDemonstration", + "type": "object", + "additionalProperties": False, + }, + "description": None, + "strict": True, + }, + }, + ) as stream: + for chunk in stream: + print(chunk.data.choices[0].delta.content, end="") + + +if __name__ == "__main__": + main() diff --git a/examples/structured_outputs.py b/examples/structured_outputs_with_pydantic.py similarity index 100% rename from examples/structured_outputs.py rename to examples/structured_outputs_with_pydantic.py