Skip to content

Structured JSON Output

Output well-formatted JSON. Here we're letting the model come up with its own data, but the same prompt would work with RAG contents as well. We've incorporate a pre-filled response to the model to ensure we get json back, as described in our documentation.

User Prompt Template

{Decription of desired output}.
You MUST answer in JSON format only. 
Please follow the output schema below.

Output Schema:
{
    "key1": "value1",
    "key2": "value2"
}

Example

Amazon Nova Lite User Prompt

Provide 5 examples of the best selling full-frame cameras in past three years.
Follow the Output Schema as described below.
Output Schema:
{
"name" : <string, the name of product>,
"brand" : <string, the name of product>,
"price" : <integer price>,
"summary": <string, the product summary>
}
Only Respond in Valid JSON, without Markdown

Amazon Nova Lite Assistant Prefilled Response

"```json"

Amazon Nova Lite Sample Response

Response

[
  {
    "name": "Canon EOS R5",
    "brand": "Canon",
    "price": "$3,899",
    "summary": "The Canon EOS R5 is a high-end full-frame mirrorless camera known for its 45MP sensor, 8K video recording, and advanced autofocus capabilities."
  },
  {
    "name": "Sony Alpha A7R IV",
    "brand": "Sony",
    "price": "$3,499",
    "summary": "The Sony Alpha A7R IV is a top-tier full-frame mirrorless camera featuring a 61MP sensor, 8K video, and excellent low-light performance."
  },
  {
    "name": "Nikon Z7 II",
    "brand": "Nikon",
    "price": "$2,999",
    "summary": "The Nikon Z7 II is a professional full-frame mirrorless camera with a 45.7MP sensor, dual EXPEED 6 processors, and excellent image quality."
  },
  {
    "name": "Canon EOS R6",
    "brand": "Canon",
    "price": "$2,499",
    "summary": "The Canon EOS R6 is a versatile full-frame mirrorless camera with a 20.1MP sensor, capable of 20fps shooting, and 4K video recording."
  },
  {
    "name": "Sony Alpha A7 III",
    "brand": "Sony",
    "price": "$1,999",
    "summary": "The Sony Alpha A7 III is a popular full-frame mirrorless camera featuring a 24.2MP sensor, fast autofocus, and 10fps continuous shooting."
  }
]
```

API Request

import boto3
import json

bedrock_runtime = boto3.client(
            service_name="bedrock-runtime",
            region_name="us-west-2"
        )


response = bedrock_runtime.converse(
                modelId='us.amazon.nova-lite-v1:0',
                messages = [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": """Provide 5 examples of the best selling full-frame cameras in past three years.

You MUST answer in JSON format only. Please follow the output schema below.

**Output Schema:**
```json
[
  {
    "name": "string, the name of the camera",
    "brand": "string, the brand name",
    "price": "integer or string with price",
    "summary": "string, brief product description"
  }
]

DO NOT provide any preamble text. Return ONLY the JSON object.""" } ] }, { "role": "assistant", "content": [ { "text": "json" } ] } ], inferenceConfig={ "temperature": 0.1, "topP": .99, "maxTokens": 1000, "stopSequences": [""] } )

print(json.dumps(response, indent=2)) ```

aws bedrock-runtime converse \
  --model-id "us.amazon.nova-lite-v1:0" \
  --messages '[
    {
      "role": "user",
      "content": [
        {
          "text": "Provide 5 examples of the best selling full-frame cameras in past three years.\nFollow the Output Schema as described below:\nOutput Schema:\n{\n\"name\" : <string, the name of product>,\n\"brand\" : <string, the name of product>,\n\"price\" : <integer price>,\n\"summary\": <string, the product summary>\n}\nOnly Respond in Valid JSON, without Markdown"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "```json"
        }
      ]
    }
  ]' \
  --inference-config '{
    "temperature": 0.7,
    "topP": 0.99,
    "maxTokens": 512
  }' \
  --region us-west-2
{
  "modelId": "us.amazon.nova-lite-v1:0",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "Provide 5 examples of the best selling full-frame cameras in past three years.\nFollow the Output Schema as described below:\nOutput Schema:\n{\n\"name\" : <string, the name of product>,\n\"brand\" : <string, the name of product>,\n\"price\" : <integer price>,\n\"summary\": <string, the product summary>\n}\nOnly Respond in Valid JSON, without Markdown"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "```json"
        }
      ]
    }
  ],
  "inferenceConfig": {
    "temperature": 0.7,
    "topP": 0.99,
    "maxTokens": 512
  }
}