Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Get Prompts doesn't work

I have created small application:

 

from flask import Request, jsonify
import vertexai
from vertexai.preview import prompts

vertexai.init(project="vertex-cs", location="us-central1")
prompt_id = "8376007562385948672"

def hello_http(request: Request):
if request.method != "GET":
return "Method Not Allowed", 405

try:
prompt = prompts.get("8376007562385948672")
 
return f"Prompt fetched successfully:\n{str(prompt)}", 200

except Exception as e:
print(" Error:", e)
return jsonify({ "error": str(e) }), 500
 
Earlier I have checked also whether prompt id exists. 
And received a list of prompts ids. 
{
"prompts": [
{
"prompt_id": "8376007562385948672"
},
}
 
The error which Im receiving is: 
{
"error": "value must not be empty"
}
 
it looks like prompt doesn't exist or its empty, but it exists and is not empty. 
{
"error": {
"message": "Multi-turn prompts are not supported yet.",
"source": "get_prompt_by_id",
"traceback": [
" _populate_fields_from_metadata(prompt=prompt, metadata=metadata)",
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^",
" File \"/layers/google.python.pip/pip/lib/python3.13/site-packages/vertexai/prompts/_prompt_management.py\", line 608, in _populate_fields_from_metadata",
" raise ValueError(\"Multi-turn prompts are not supported yet.\")",
"ValueError: Multi-turn prompts are not supported yet."
],
"type": "ValueError"
}
}
 
Is there any way to do prompt as singleturn?
Question answer thats all?
 
 
0 3 72
3 REPLIES 3

Hi olekwojcik1,

Welcome to the Google Cloud Community!

It looks like the prompt you're trying to use is being treated as a "multi-turn" prompt. Currently, when using the vertexai.preview.prompts only works with "single-turn" prompts. So even if your prompt shows up in the list and has content, if it's saved or written in a way that looks like a conversation with multiple steps, the API will give you an error.

Here are some best practices for single-turn prompts: 

  • Use GenerativeModel instead of prompts.get() for direct, single-turn questions, as it's the recommended way to interact with Gemini models.
  • Use vertexai.generative_models.GenerativeModel and call generate_content() directly to send your question, as this method supports single-turn interactions by default.

For more detailed information on prompt management, you may also refer to this documentation.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Yes, it seems to be working fine. But currently facing another issue: 

 "gemini_response": "Error generating Gemini response: 404 Publisher Model `projects/vertex-cs/locations/us-central1/publishers/google/models/gemini-1.5-flash-001` was not found or your project does not have access to it. Please ensure you are using a valid model version. For more information, see: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versions",
    "id": "1783371026613141504",

code example:

from flask import Request, jsonify
import vertexai
from vertexai.generative_models import GenerativeModel

vertexai.init(project="vertex-cs", location="us-central1")

def generate_gemini_response_for_hardcoded_prompt(prompt_text: str) -> dict:
gemini_response_text = "Failed to generate Gemini response."

try:
model = GenerativeModel("gemini-1.5-flash-002")
gemini_response = model.generate_content(prompt_text)
gemini_response_text = gemini_response.text
except Exception as e:
gemini_response_text = f"Error generating Gemini response: {str(e)}"

return {
"content": prompt_text,
"gemini_response": gemini_response_text
}

def hello_http(request: Request):
if request.method != "GET":
return "Method Not Allowed", 405

try:
my_hardcoded_prompt = "Write a short, funny rhyme about programming in Python."
prompt_and_response = generate_gemini_response_for_hardcoded_prompt(my_hardcoded_prompt)

response_data = {
"message": "Successfully processed hardcoded prompt with Vertex AI.",
"prompt_details": {
"id": "hardcoded_rhyme_prompt_001",
"display_name": "Python Rhyme Generator",
"content": prompt_and_response["content"],
"gemini_response": prompt_and_response["gemini_response"]
}
}

return jsonify(response_data), 200

except Exception as e:
print(f" General error in hello_http function: {e}")
return jsonify({ "error": f"An unexpected error occurred: {str(e)}" }), 500