Error with request for structured response nodejs client (400: Invalid schema for response_format)

I keep getting BadRequestError: 400 Invalid schema for response_format 'job': {} is not of type 'string'. I’ve checked my code and my Zod schema several times. I can’t find anything. I’ve also shown this to other people too.

Here’s my code page:

import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";
import { OPENAI_API_KEY } from '$env/static/private';

	const jobCategories = [
		{
			"slug": "customer-support",
			"label": "Customer Support",
			"sub": [
				{
					"slug": "customer-service",
					"label": "Customer Service"
				},
				{
					"slug": "support-engineer",
					"label": "Support Engineer"
				},
				{
					"slug": "technical-support",
					"label": "Technical Support"
				},
				{
					"slug": "helpdesk",
					"label": "Helpdesk"
				},
				{
					"slug": "customer-success",
					"label": "Customer Success"
				},
				{
					"slug": "account-management",
					"label": "Account Management"
				}
			]
		},
		{
			"slug": "product",
			"label": "Product",
			"sub": [
				{
					"slug": "product-management",
					"label": "Product Management"
				},
				{
					"slug": "product-design",
					"label": "Product Design"
				},
				{
					"slug": "product-marketing",
					"label": "Product Marketing"
				},
				{
					"slug": "product-analytics",
					"label": "Product Analytics"
				},
				{
					"slug": "product-owner",
					"label": "Product Owner"
				},
				{
					"slug": "product-development",
					"label": "Product Development"
				}
			]
		},
		{
			"slug": "dev",
			"label": "Dev",
			"sub": [
				{
					"slug": "software-engineering",
					"label": "Software Engineering"
				},
				{
					"slug": "web-development",
					"label": "Web Development"
				},
				{
					"slug": "mobile-development",
					"label": "Mobile Development"
				},
				{
					"slug": "backend-development",
					"label": "Backend Development"
				},
				{
					"slug": "frontend-development",
					"label": "Frontend Development"
				},
				{
					"slug": "fullstack-development",
					"label": "Fullstack Development"
				},
				{
					"slug": "devops",
					"label": "DevOps"
				},
				{
					"slug": "data-science",
					"label": "Data Science"
				},
				{
					"slug": "artificial-intelligence-machine-learning",
					"label": "AI/ML"
				},
				{
					"slug": "quality-assurance",
					"label": "Quality Assurance"
				},
				{
					"slug": "systems-administration",
					"label": "Systems Administration"
				},
				{
					"slug": "network-engineering",
					"label": "Network Engineering"
				}
			]
		},
		{
			"slug": "design",
			"label": "Design",
			"sub": [
				{
					"slug": "ui-ux-design",
					"label": "UI/UX Design"
				},
				{
					"slug": "graphic-design",
					"label": "Graphic Design"
				},
				{
					"slug": "visual-design",
					"label": "Visual Design"
				},
				{
					"slug": "interaction-design",
					"label": "Interaction Design"
				},
				{
					"slug": "product-design",
					"label": "Product Design"
				},
				{
					"slug": "user-research",
					"label": "User Research"
				},
				{
					"slug": "ux-writing",
					"label": "UX Writing"
				}
			]
		},
		{
			"slug": "marketing",
			"label": "Marketing",
			"sub": [
				{
					"slug": "digital-marketing",
					"label": "Digital Marketing"
				},
				{
					"slug": "content-marketing",
					"label": "Content Marketing"
				},
				{
					"slug": "social-media-marketing",
					"label": "Social Media Marketing"
				},
				{
					"slug": "email-marketing",
					"label": "Email Marketing"
				},
				{
					"slug": "seo",
					"label": "SEO"
				},
				{
					"slug": "sem",
					"label": "SEM"
				},
				{
					"slug": "growth-marketing",
					"label": "Growth Marketing"
				},
				{
					"slug": "marketing-automation",
					"label": "Marketing Automation"
				},
				{
					"slug": "marketing-analytics",
					"label": "Marketing Analytics"
				}
			]
		},
		{
			"slug": "sales",
			"label": "Sales",
			"sub": [
				{
					"slug": "sales-development",
					"label": "Sales Development"
				},
				{
					"slug": "account-executive",
					"label": "Account Executive"
				},
				{
					"slug": "sales-manager",
					"label": "Sales Manager"
				},
				{
					"slug": "business-development",
					"label": "Business Development"
				},
				{
					"slug": "sales-operations",
					"label": "Sales Operations"
				},
				{
					"slug": "customer-success",
					"label": "Customer Success"
				}
			]
		},
		{
			"slug": "finance",
			"label": "Finance",
			"sub": [
				{
					"slug": "financial-analyst",
					"label": "Financial Analyst"
				},
				{
					"slug": "accountant",
					"label": "Accountant"
				},
				{
					"slug": "bookkeeper",
					"label": "Bookkeeper"
				},
				{
					"slug": "controller",
					"label": "Controller"
				},
				{
					"slug": "cfo",
					"label": "CFO"
				},
				{
					"slug": "investor-relations",
					"label": "Investor Relations"
				}
			]
		},
		{
			"slug": "operations",
			"label": "Operations",
			"sub": [
				{
					"slug": "project-management",
					"label": "Project Management"
				},
				{
					"slug": "operations-manager",
					"label": "Operations Manager"
				},
				{
					"slug": "process-improvement",
					"label": "Process Improvement"
				},
				{
					"slug": "supply-chain-management",
					"label": "Supply Chain Management"
				},
				{
					"slug": "logistics",
					"label": "Logistics"
				},
				{
					"slug": "customer-service",
					"label": "Customer Service"
				}
			]
		},
		{
			"slug": "human-resources",
			"label": "Human Resources",
			"sub": [
				{
					"slug": "recruiter",
					"label": "Recruiter"
				},
				{
					"slug": "hr-manager",

					"label": "HR Manager"
				},
				{
					"slug": "talent-acquisition",
					"label": "Talent Acquisition"
				},
				{
					"slug": "compensation-and-benefits",
					"label": "Compensation and Benefits"
				},
				{
					"slug": "employee-relations",
					"label": "Employee Relations"
				},
				{
					"slug": "training-and-development",
					"label": "Training and Development"
				}
			]
		},
		{
			"slug": "legal",
			"label": "Legal",
			"sub": [
				{
					"slug": "lawyer",
					"label": "Lawyer"
				},
				{
					"slug": "paralegal",
					"label": "Paralegal"
				},
				{
					"slug": "legal-counsel",
					"label": "Legal Counsel"
				},
				{
					"slug": "compliance",
					"label": "Compliance"
				},
				{
					"slug": "intellectual-property",
					"label": "Intellectual Property"
				},
				{
					"slug": "contract-review",
					"label": "Contract Review"
				}
			]
		},
		{
			"slug": "writing",
			"label": "Writing",
			"sub": [
				{
					"slug": "content-writer",
					"label": "Content Writer"
				},
				{
					"slug": "copywriter",
					"label": "Copywriter"
				},
				{
					"slug": "technical-writer",
					"label": "Technical Writer"
				},
				{
					"slug": "editor",
					"label": "Editor"
				},
				{
					"slug": "proofreader",
					"label": "Proofreader"
				},
				{
					"slug": "ghostwriter",
					"label": "Ghostwriter"
				}
			]
		},
		{
			"slug": "management",
			"label": "Management",
			"sub": [
				{
					"slug": "ceo",
					"label": "CEO"
				},
				{
					"slug": "cmo",
					"label": "CMO"
				},
				{
					"slug": "coo",
					"label": "COO"
				},
				{
					"slug": "cfo",
					"label": "CFO"
				},
				{
					"slug": "cto",
					"label": "CTO"
				},
				{
					"slug": "vp-of-engineering",
					"label": "VP of Engineering"
				},
				{
					"slug": "vp-of-product",
					"label": "VP of Product"
				},
				{
					"slug": "vp-of-marketing",
					"label": "VP of Marketing"
				},
				{
					"slug": "vp-of-sales",
					"label": "VP of Sales"
				},
				{
					"slug": "vp-of-operations",
					"label": "VP of Operations"
				}
			]
		},
		{
			"slug": "other",
			"label": "Other",
			"sub": [
				{
					"slug": "business-analyst",
					"label": "Business Analyst"
				},
				{
					"slug": "data-analyst",
					"label": "Data Analyst"
				},
				{
					"slug": "researcher",
					"label": "Researcher"
				},
				{
					"slug": "consultant",
					"label": "Consultant"
				},
				{
					"slug": "entrepreneur",
					"label": "Entrepreneur"
				},
				{
					"slug": "freelancer",
					"label": "Freelancer"
				},
				{
					"slug": "virtual-assistant",
					"label": "Virtual Assistant"
				}
			]
		},
		{
			"slug": "web3",
			"label": "Web3",
			"sub": [
				{
					"slug": "blockchain-developer",
					"label": "Blockchain Developer"
				},
				{
					"slug": "smart-contract-developer",
					"label": "Smart Contract Developer"
				},
				{
					"slug": "dapp-developer",
					"label": "DApp Developer"
				},
				{
					"slug": "nft-developer",
					"label": "NFT Developer"
				},
				{
					"slug": "web3-engineer",
					"label": "Web3 Engineer"
				},
				{
					"slug": "crypto-trader",
					"label": "Crypto Trader"
				},
				{
					"slug": "defi-developer",
					"label": "DeFi Developer"
				},
				{
					"slug": "dao-manager",
					"label": "DAO Manager"
				}
			]
		}
	];
	const jobCategoriesEnum = jobCategories.map(cat => cat.slug);
	const jobSubcategoriesEnum = jobCategories.flatMap(cat => cat.sub.map(sub => sub.slug));
	const jobZodSchema = z.object({
		id: z.string(),
		slug: z
			.string()
			.describe("Slug must be URL-friendly"),
		title: z.string().describe("Job title is required"),
		description: z.string().describe(`Required. Must be in HTML tags. Example: <div>
         <h2>About Us</h2>
         <p>We are a forward-thinking AI technology company dedicated to pushing the boundaries of artificial intelligence and machine learning solutions. Our mission is to develop innovative AI technologies that solve complex challenges across industries, from healthcare and finance to environmental sustainability. We foster a culture of continuous learning, creativity, and collaboration, empowering our team to drive technological innovation.</p>
     </div>

     <div>
         <h2>Job Summary</h2>
         <p>We are seeking a highly motivated Junior AI Agent to join our innovative development team. In this role, you will be responsible for assisting in the creation and maintenance of AI-driven applications and solutions. This is an excellent opportunity for an entry-level AI specialist to grow their skills and contribute to cutting-edge projects.</p>
     </div>

     <div>
         <h2>Responsibilities</h2>
         <p>Assist in developing new AI-powered features and applications</p>
         <p>Build and train machine learning models for various use cases</p>
         <p>Translate complex business requirements into AI-driven solutions</p>
         <p>Optimize AI models for maximum performance and accuracy</p>
         <p>Collaborate with data scientists, developers, and stakeholders to ensure quality and timely delivery of projects</p>
         <p>Participate in model reviews and contribute to a collaborative work environment</p>
         <p>Troubleshoot and debug AI model performance issues</p>
     </div>

     <div>
         <h2>Required Skills and Qualifications</h2>
         <p>Basic understanding of machine learning concepts and algorithms</p>
         <p>Proficiency in programming languages such as Python, R, or Julia</p>
         <p>Familiarity with AI and machine learning frameworks (e.g., TensorFlow, PyTorch)</p>
         <p>Ability to understand business requirements and translate them into technical AI solutions</p>
         <p>Strong analytical and problem-solving skills</p>
         <p>Excellent communication and teamwork abilities</p>
     </div>

     <div>
         <h2>Preferred Skills</h2>
         <p>Experience with deep learning and neural network architectures</p>
         <p>Familiarity with natural language processing (NLP) techniques</p>
         <p>Knowledge of data preprocessing and feature engineering</p>
         <p>Experience with version control systems, particularly Git</p>
         <p>Understanding of cloud AI platforms (e.g., AWS AI, Google Cloud AI, Azure AI)</p>
     </div>

     <div>
         <h2>Education and Experience</h2>
         <p>Bachelor's degree in Computer Science, Artificial Intelligence, Data Science, or a related field, or equivalent practical experience</p>
         <p>Previous internship or project experience in AI development is a plus</p>
     </div>
     <div>
         <h2>Work Environment</h2>
         <p>Collaborative and supportive team culture</p>
         <p>Opportunities for professional growth and development in AI technologies</p>
         <p>Flexible working hours and remote work options available</p>
         <p>Access to the latest AI tools and technologies</p>
         <p>Engaging and innovative projects across various domains and industries</p>
     </div>
    
     <div>
         <h2>Additional Notes</h2>
         <p>We accept applicants from anywhere and everywhere, regardless of race, color, religion.</p>
         <p>Think you're not up to the task? Apply. It might just work out.</p>
     </div>`),
		schedule: z.enum(["full_time", "part_time", "contract"]).describe("Job schedule is required"),
		level: z.enum([
			"internship",
			"entry_level",
			"junior",
			"midlevel",
			"senior",
			"executive",
			"cofounder",
		]).describe("Job level is required"),
		min_salary: z.union([z.string(), z.undefined()]).describe("Only the currency numbers, but as a string e.g 20000"),
		max_salary: z.union([z.string(), z.undefined()]).describe("Only the currency numbers, but as a string e.g 20000. Maximum must be equal to or greater than minimum salary."),
		salary_currency: z.union([z.string(), z.undefined()]).describe("Only currency code"),
		apply_url: z.string().url("Invalid URL").describe("Should be included already"),
		source: z.string().describe("Should be included already"),
		type: z.enum(["standard", "premium", "free"]).describe("Job Type is required"),
		category: z.enum(jobCategoriesEnum).describe("Required. Choose category sensibly"),
		subcategory: z.enum(jobSubcategoriesEnum).describe("Required. Only choose a sub category that falls under a category sensibly"),
		tags: z.array(z.string())
			.describe("Between 5 and 30 tags. Choose tags based on possible search or filter terms."),
		perks: z.array(z.string().describe("Perk description cannot exceed 50 characters").describe("Only add perk if explicitly mentioned in job data")),
		skills: z
			.array(z.string().min(1, "Skill cannot be empty").describe(/^[A-Z].*/, "Skills must start with a capital letter"))
			.describe("Add skills only explicitly mentioned in the job data, or implied by the job title or in the job description, or in the role."),
		posted_at: z.string().datetime().describe(
			"Should be included already"
		),
		expires_at: z.string().datetime().describe(
			"Should be included already"
		),
	})

const openai = new OpenAI({
	apiKey: OPENAI_API_KEY
});


/**
 *
 * @param {ImportJobType} dataObj
 * @returns {Promise<ImportJobType>}
 */
const refineJob = async (dataObj) => {
	const completion = await openai.beta.chat.completions.parse({
		// model: "gpt-4o-2024-08-06",
		model: "gpt-4o-mini",
		messages: [
			{ role: "system", content: `You are a specialized AI job data refinement agent tasked with transforming raw job listing data into a meticulously structured JSON object matching the specified job schema.

Core Objectives:
- Validate and complete ALL required fields
- Ensure 100% compliance with the schema specification
- Maintain data integrity and accuracy
- Provide intelligent inference for missing information

Field-Specific Processing Guidelines:

1. ID and Slug:
- Preserve original ID exactly
- Create URL-friendly slug if not provided
- Ensure unique identifier consistency

2. Title:
- Standardize to professional, clear job title
- Correct capitalization and formatting
- Remove unnecessary decorative text

3. Description:
- If description is empty, generate a comprehensive HTML-formatted description
- Use only allowed HTML tags: p, b, h1-h6, i, t, br, ul, ol, li, div
- Ensure semantic structure with clear sections
- Maintain professional tone and clarity

4. Schedule:
- Map to exact schema enum: full_time, part_time, or contract
- Infer from context if not explicitly stated

5. Level:
- Categorize precisely: internship, entry_level, junior, midlevel, senior, executive, or cofounder
- Use contextual clues from description and requirements

6. Salary:
- Extract min and max salary as strings
- Identify and standardize salary currency

7. Categories and Tags:
- Map to a provided applicable category and subcategory enums
- Generate relevant, lowercase tags in order of relevance
- Identify key skills and perks

8. Dates:
- Use current date if posting date is unclear
- Apply standard expiration policy (typically 30 days from posting)

Validation Constraints:
- ALL required fields MUST be populated
- No additional properties allowed
- Skills limited to 10 or less, sentence case
- Perks as short phrases/words
- Consistent formatting across all fields

Inference Confidence:
- Prioritize accuracy and realistic estimation

Return the refined job object strictly adhering to the specified JSON schema, demonstrating comprehensive data processing and intelligent information enhancement.
PS: Return only the JSON object. Nothing more nothing less` },
			{ role: "user", content: JSON.stringify(dataObj) },
		],
		top_p: 1.0,
		temperature: 0.2,
		response_format: zodResponseFormat(jobZodSchema, "job"),
	});

	const event = completion.choices[0].message.parsed;
	console.log(event)
	return event
}


export {refineJob}

Help would be greatly appreciated. I’m out of ideas :sleepy: