What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Field 'base' can't be blank, blog posting script error

Field 'base' can't be blank, blog posting script error

tlawrence99
Visitor
2 0 0

Hi community, 

 

I'm running a python script that writes blog posts for my store using OpenAI. The script is producing the blog posts and is pulling relevant products from my store. However, I'm getting this error message that prevents the blogs from posting: 

 

Error creating blog post:

- can't be blank

Full error details:

Field 'base' - can't be blank

 

I cannot find the base field it mentions in the shopify api language. Any ideas? below is the relevant part of the code:

 

# Generate the blog content
blog_content = generate_blog_content(prompt)

def generate_blog_title(keyword):
primed_title_prompt = f"Create an engaging and SEO-optimized blog title about {keyword}:"

response = openai.Completion.create(
engine="text-davinci-002",
prompt=primed_title_prompt,
max_tokens=20,
temperature=0.7,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)

return response.choices[0].text.strip()

post_title = generate_blog_title(selected_keyword)

# Post-process the generated content: add subheadings, meta description, and image alt tags
# Extract keywords from the content using SpaCy
# ... (rest of the code)


# Post-process the generated content: add subheadings, meta description, and image alt tags
# Extract keywords from the content using SpaCy
doc = nlp(blog_content)
extracted_keywords = [token.lemma_ for token in doc if token.is_alpha and not token.is_stop]
extracted_keywords = list(set(extracted_keywords))


# Add subheadings using extracted keywords
subheadings = random.sample(extracted_keywords, min(3, len(extracted_keywords)))
for subheading in subheadings:
blog_content = blog_content.replace(subheading, f'<h2>{subheading.capitalize()}</h2>', 1)

post_content = f"<p>{blog_content}</p>"

# Create a blog post on Shopify
import requests
import random

product_images = [
{"url": "https://www.example.com/image1.jpg", "tags": ["ethical alternatives"]},

# Add more product images with their corresponding tags
]

def get_random_image():
return random.choice(product_images)["url"]

def get_relevant_product_image(tags):
relevant_images = [img for img in product_images if any(tag in img["tags"] for tag in tags)]

if not relevant_images:
return get_random_image()

return random.choice(relevant_images)["url"]

# Replace the placeholder functions in your previous code with these functions

# Extract the first paragraph as an excerpt and search engine listing preview
first_paragraph = post_content.split('</p>')[0] + '</p>'

image_url = get_relevant_product_image(selected_keyword.split())

print("post_title:", post_title)
print("post_content:", post_content)
print("selected_keyword:", selected_keyword)
print("image_url:", image_url)
print("first_paragraph:", first_paragraph)

new_post = {
"article": {
"title": post_title,
"author": "Thomas Lawrence",
"blog_id": removedbutthispartis,
"tags": selected_keyword,
}
}


print(new_post) # Add this line
with shopify.Session.temp(shop_url, '2023-01', password):
post = shopify.Article.create(new_post)
print("Shopify API response:", post.to_dict()) # Add this line to print the Shopify API response

if post.is_valid():
print(f"Successfully created blog post: {post_title}")
else:
print("Error creating blog post:")
for error in post.errors.full_messages():
print(f"- {error}")
print("Full error details:")
for field, error_list in post.errors.errors.items():
for error in error_list:
print(f"Field '{field}' - {error}")

Reply 1 (1)

ShopifyDevSup
Shopify Staff
1453 238 525

Hi @tlawrence99,

 

We're not able to replicate any error messages similar to that when testing creating blogs using the REST Admin API so you may need to debug further to identify what exactly is being sent and to which endpoint or reach out to the support channels of whichever Python library is being used.

 

Hope you have a great day

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog