A Json response should be json parsable

If i ask for a json object to be returned i do not want to see it start with json and end with

```json
[Json here]
```

I would rather not have to do this everytime.

response = response.replace("```json","")
response = response.replace("```", "")

Thanks :heart:

3 Likes

Thank you for feedback on how the API returns JSON responses. I’ve passed this along – we’ll take a look.

1 Like

Looks like Markdown to provide the response. If shown in a Markdown viewer those ticks would be gone and only JSON shown.

It seems that the response is always wrapped as Markdown at the moment instead of the requested format directly.

Not practical for using the API which should deliver the raw response.

I quickly set up a test case for Gemini Pro Vision with the following info:
Prompt: “Parse the time and city from the airport board shown in this image into a list, in JSON”
Image: https://ai.google.dev/static/docs/images/timetable.png

Response received: JSON array of objects
[
{
“time”: “10:50”,
“city”: “Moscow”
},
{
“time”: “11:05”,
“city”: “Edinburgh”
},
{
“time”: “11:05”,
“city”: “Bucharest”
},
{
“time”: “11:10”,
“city”: “Kiev”
},
{
“time”: “11:30”,
“city”: “Bristol”
},
{
“time”: “11:35”,
“city”: “Dublin”
},
{
“time”: “11:45”,
“city”: “East Midlands”
},
{
“time”: “12:15”,
“city”: “Sofia”
},
{
“time”: “12:30”,
“city”: “London”
},
{
“time”: “12:30”,
“city”: “Newcastle”
},
{
“time”: “12:40”,
“city”: “St Petersburg”
},
{
“time”: “12:40”,
“city”: “London”
},
{
“time”: “12:45”,
“city”: “Manchester”
}
]

See here: generative-ai/tests/Mscc.GenerativeAI/GoogleAi_GeminiProVision_Should.cs at 08d7954feab5217f60275c7d61dec24406a3e464 · mscraftsman/generative-ai · GitHub

Hope this helps.

Hi @LindaLawton

Does this happen when using JSON mode?

2 Likes

This question predates Json Mode.

I haven’t had time to figure out how to add that to my current python project

I’m still striping garbage.

1 Like

Agree, the google dev docs is a maze to navigate through.

You can set json mode by simply passing response_mime_type = "application/json" in generationConfig.

Here’s the sample code I wrote:

import google.generativeai as genai

import json

model = genai.GenerativeModel("gemini-1.5-pro-latest")
json_schema = {
    "type": "object",
    "properties": {
        "characters": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1,
            "description": "List of character names in the story.",
        },
        "story": {
            "type": "string",
            "description": "The story in which all the characters are involved",
        },
    },
    "required":["characters", "story"]
}
response = model.generate_content(
    f"Write a short story and list its characters in the following JSON schema: {json.dumps(json_schema)}",
    generation_config=genai.types.GenerationConfig(
        # Only one candidate for now.
        candidate_count=1,
        stop_sequences=["x"],
        max_output_tokens=700,
        temperature=1.0,
        response_mime_type="application/json",
    ),
)

print(response)

and its output:

response:
GenerateContentResponse(
    done=True,
    iterator=None,
    result=glm.GenerateContentResponse({'candidates': [{'content': {'parts': [{'text': '{"characters": ["Amelia", "Oliver", "Jasper"], "story": "Amelia nervously paced the cobblestone streets, her eyes searching the crowd for Oliver. He was late, as usual, but today it felt different. A shadow fell over her and she turned to see Jasper, a man she knew only through Oliver\'s stories. \\"He won\'t be coming,\\" Jasper said, his voice low. \\"He\'s gone.\\""}\n\n'}], 'role': 'model'}, 'finish_reason': 1, 'index': 0, 'safety_ratings': [{'category': 9, 'probability': 1, 'blocked': False}, {'category': 8, 'probability': 1, 'blocked': False}, {'category': 7, 'probability': 1, 'blocked': False}, {'category': 10, 'probability': 1, 'blocked': False}], 'token_count': 0, 'grounding_attributions': []}]}),
)
2 Likes

My first post on the forum!

Hey Linda, thanks for your question (and thanks for your code snippet, @sps)

Here are two more examples of activating JSON mode from the cookbook, in case these help too.

Python: cookbook/quickstarts/JSON_mode.ipynb at main · google-gemini/cookbook · GitHub

curl: cookbook/quickstarts/rest/JSON_mode_REST.ipynb at main · google-gemini/cookbook · GitHub

These should always give you output in proper JSON. If they don’t, please lmk!

2 Likes

I am working with gemini 1.5 pro latest but I got error when i tried to add response_mime_type=“application/json”
it gives me error.

And I don’t get proper Json output

Hi,
What’s the error message?
Are you using the latest version of the SDK? Which SDK are you using?

Regards, JoKi

i get 404 on image link https://ai.google.dev/static/docs/images/timetable.png

hey how are you lol and who am i talking to ?

Hm, looks like the image has either been removed or moved to another URL.

I’m using gemini-1.5-flash-001, in JSON return mode.
The JSON I ask for, gets returned as type , which I can’t parse as a JSON into a dict. This is rather inconvenient.
I’m using the python-sdk, latest.
Cheers,
Kumar

Same here, I tried many things. It keeps appending " ```json". A cost effective workaround for now has been a GPT3.5 assistant to take the output JSON and fix it. That seems to work most of the time. Hope there is a solution soon.

Hi @Kumar_Ramanathan,

While gemini-1.5-flash models only accept a text description of the JSON you want back, gemini-1.5-pro models support “controlled generation” (aka “constrained decoding”). This allows you to pass a schema object (or a python type equivalent) and the output will strictly follow that schema.

@Kumar_Ramanathan , @Darry_Shan

Also as per cookbook I made few changes to the top commented attached code with Gemini-1.5-flash

Thanks

Ahh, @Siva_Sravana_Kumar_N , perfect, thanks for the changed code and the quick reply ! This works like a charm, much obliged !
Kumar

1 Like