I still have an issue with 400 error, please tell me if you can see any obvious errors in my code.
I’ve tried with ensuring that the file is fully uploaded and accessible, but with the same error.
Let me provide a full example of my test code:
#!/bin/bash
set -euo pipefail
# Replace with your actual API key
API_KEY=""
# Define the single file and its MIME type.
FILE="test.csv"
MIME_TYPE="text/csv"
# Verify the file exists.
if [[ ! -f "$FILE" ]]; then
echo "Error: File '$FILE' not found."
exit 1
fi
# Calculate file size in bytes.
NUM_BYTES=$(wc -c < "$FILE")
echo "Uploading $FILE ($NUM_BYTES bytes, MIME type: $MIME_TYPE)..."
# Upload the file and capture the response.
RESPONSE=$(curl -s "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${API_KEY}" \
-H "X-Goog-Upload-Command: start, upload, finalize" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d "{\"file\": {\"display_name\": \"${FILE}\"}}" \
--data-binary "@${FILE}")
# Parse the file URI from the response.
FILE_URI=$(echo "$RESPONSE" | jq -r '.file.uri')
if [[ "$FILE_URI" == "null" || -z "$FILE_URI" ]]; then
echo "Error: Failed to retrieve file URI. Response: $RESPONSE"
exit 1
fi
# Define the file URI as FILE_URI_0 (as in the original sample).
export FILE_URI_0="${FILE_URI}"
echo "Uploaded file URI: $FILE_URI_0"
# Prepare the JSON payload with two separate content objects.
PAYLOAD=$(cat <<EOF
{
"contents": [
{
"role": "user",
"parts": [
{
"fileData": {
"fileUri": "${FILE_URI_0}",
"mimeType": "text/csv"
}
}
]
},
{
"role": "user",
"parts": [
{
"text": "Calculate the sum of the effect column"
}
]
}
],
"generationConfig": {
"temperature": 1,
"topK": 40,
"topP": 0.95,
"maxOutputTokens": 8192,
"responseMimeType": "text/plain"
}
}
EOF
)
echo "Payload:"
echo "$PAYLOAD" | jq '.'
# Send the generateContent request.
echo "Sending generateContent request..."
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}" \
-H 'Content-Type: application/json' \
-d "$PAYLOAD"
echo "Done."
With the following csv:
date,effect,spend
2023-01-01, 100, 50
2023-02-01, 150, 100
2023-03-01, 100, 50
I get the following response:
Uploading test.csv ( 20934 bytes, MIME type: text/csv)...
Uploaded file URI: https://generativelanguage.googleapis.com/v1beta/files/brcxbdn87y8
Payload:
{
"contents": [
{
"role": "user",
"parts": [
{
"fileData": {
"fileUri": "https://generativelanguage.googleapis.com/v1beta/files/brcxbdn87y8",
"mimeType": "text/csv"
}
}
]
},
{
"role": "user",
"parts": [
{
"text": "Calculate the sum of the effect column"
}
]
}
],
"generationConfig": {
"temperature": 1,
"topK": 40,
"topP": 0.95,
"maxOutputTokens": 8192,
"responseMimeType": "text/plain"
}
}
Sending generateContent request...
{
"error": {
"code": 400,
"message": "File https://generativelanguage.googleapis.com/v1beta/files/brcxbdn87y8 not exist in the Gemini API.",
"status": "INVALID_ARGUMENT"
}
}
Done.