r/MistralAI•
cs378

How can I make Mistral to respond in JSON Schema Format in Python?

How can I make Mistral to respond in JSON Schema Format in Python?

Please help me with my Python code below. I copied this code directly from MistralAI Studio. How can I get back in JSON format?

import base64
#import os
from mistralai import Mistral


#api_key = os.environ["MISTRAL_API_KEY"]
api_key = "API_CODE"


client = Mistral(api_key=api_key)


def
 encode_file(file_path):
    with open(file_path, "rb") as pdf_file:
        return base64.b64encode(pdf_file.read()).decode('utf-8')


file_path = "invoice.pdf"
base64_file = encode_file(file_path)


ocr_response = client.ocr.process(
    model="mistral-ocr-latest",
    document={
      "type": "document_url",
      "document_url": 
f
"data:application/pdf;base64,{base64_file}" 
    },
    document_annotation_format={
        "type": "json_schema",
        "json_schema": {
            "jsonSchema": {
            "name": "response_schema",
            "schemaDefinition": {
                "type": "object",
                "properties": {
                    "customer": {
                        "type": "object",
                        "properties": {
                            "billTo": {
                                "type": "string",
                                "description": "This field contains the customer name."
                            },
                            "shipTo": {
                                "type": "string",
                                "description": "This field contains the ship to informations."
                            }
                        }
                    },
                    "invoice": {
                        "type": "object",
                        "properties": {
                            "invDate": {
                                "type": "string"
                            },
                            "InvNumber": {
                                "pattern": "[0-9]",
                                "minLength": 6,
                                "type": "string",
                                "maxLength": 8,
                                "description": "This field contains the invoice number."
                            }
                        },
                        "required": [
                            "invDate",
                            "InvNumber"
                        ]
                    },
                    "saleAmount": {
                        "type": "number"
                    },
                    "tax": {
                        "type": "number"
                    },
                    "balance": {
                        "type": "number"
                    },
                    "page": {
                        "type": "number",
                        "description": "The page number of the file."
                    }
                },
                "required": [
                    "customer",
                    "invoice",
                    "saleAmount",
                    "tax",
                    "balance",
                    "page"
                ]
                }
            }
        },
    },
    include_image_base64=False
)


print(ocr_response)
print(type(ocr_response))
print(dir(ocr_response))

Thank you :)

Posted Jan 14, 2026, 12:09 AM UTC

10
Loading comments…