Return Valid JSON

Hi, I’m working on a workflow with these steps:

trigger → send_prompt → parse_ai_response → add_single_row.

Goal: I want to return valid JSON with fields:

{ ai_title: string, ai_description: string, ai_tags: , ai_price: number }

and write them to my Google Sheet.

Problem: OpenAI sometimes outputs extra text or markdown, and my parse step fails with errors like:

“JSON.parse failed: Expected property name or ‘}’ in JSON at position 1.”

I don’t see the Chat + JSON mode option in my OpenAI step (only davinci-002 and babbage-002).

What’s the best way in Pipedream to force valid JSON output so the parse step doesn’t break?

Thanks a lot — a short pointer would unblock me!

1 Like

Great question. I want to know too!!!

Maybe you can use a Node.js step and use the OpenAI SDK to use the Chat+ JSON feature? Or use a raw axios request to call the OpenAI API directly?

I’m glad I’m not the only one stuck on this :sweat_smile: I’ve been trying to force OpenAI in Pipedream to return strict JSON too. I’ve managed a partial workaround by running the response through a Node.js step and cleaning it up with JSON.parse, but it still feels a bit fragile.

Has anyone here found a more reliable way — maybe using the OpenAI SDK’s response_format: { type: “json_schema” } option, or some hidden trick in the Chat step to guarantee valid JSON?

Would love to hear if someone has a clean solution for this.

You can totally use the Response Format = JSON schema in OpenAI - Chat action on Pipedream

Thanks so much for the tip about the response format :pray: I’ve now set the OpenAI step to return a JSON schema (ai_title, ai_description, ai_tags, ai_price). That part works, and I can see the structured response coming through in Pipedream.

The issue I’m still running into is getting that parsed AI output (especially ai_description) to reliably show up in Google Sheets via the add_single_row step. Sometimes it’s blank, even though I can see the description in the AI response.

:point_right: Do you have any advice on the cleanest way to handle mapping / parsing between the ChatGPT response and the Google Sheets row?

Thanks so much for the suggestion! :clap:

Setting the response format to JSON Schema in the Chat step fixed everything perfectly :tada:

Everything’s now running smoothly end-to-end. Really appreciate the tip!