Passing a list in prompt using OpenAI API

I’m forming a prompt with format string, and I need to pass a list of dictionaries to that promt.

If I copy and paste the resulting prompt into chat.gpt, all works fine. But, if I run the script with that prompt I get invalid syntax (, line 1). If I remove the list parameter, the api response works fine.

Thanks in advance

Can you share your code? There is no list parameter, are you including them in the message content?


Hi novaphil, thanks for answer

def generate_code(url: str, summary: str, parameters: list, header: json, method: str):
parameter_list = json.dumps(parameters)
description = “# Write a function in python to access API that”
f" # 1. Have name: {summary}"
f" # 2. the API url: {url}"
f" # 3. the access method: {method}"
f" # 4. the header: {header}"
f" # 5. the parameters is a list of dictionaries: {parameter_list}"

response = openai.Completion.create(

this is my last change, tried to do a json.dumps that list, and, nothing happens

What error are you getting? What’s the output of print(response)

The error is
invalid syntax (, line 1)

If I paste that generated prompt into my web browser (chat.openai link) and works fine. The problem is executing that prompt directly from the python script.

This doesn’t look like an OpenAI error, but just a general Python error. Is that from the OpenAI response? Is there a line number mentioned? Does your entire python file validate (leave out your API key)?

Also not sure if this is copy-paste issue, but both the opening and ending quotes are smart-quotes. Is your code using standard quotes?
vs "

1 Like

if I remove that line, OpenAI script works fine. ANd indeed, is the response error

If you comment out the openAI code and replace with print(description) does it match what you want? Can you share the full prompt string?

yes, it does. I change the prompt, and now it works splendid.

prompt = f"Write a function in python to access API that have as name: {summary}, the API url: {url}, the access method: {method}, the header: {header} and this list of parameters: {parameters}"

Thanks a lot novaphil