import os import json from pyairtable import Api from dotenv import load_dotenv load_dotenv() def save_to_airtable(candidate_data: dict, job_role: str): """ Saves the candidate data using the exact field names identified in image_95657f.png. """ try: # Initialize the Airtable API client api = Api(os.getenv("AIRTABLE_API_KEY")) table = api.table(os.getenv("AIRTABLE_BASE_ID"), os.getenv("AIRTABLE_TABLE_NAME")) # Format the skills list into a single readable string skills_list = candidate_data.get("skills") or [] skills_string = ", ".join(skills_list) links = candidate_data.get("links") or {} # Mapping to the exact names in your Airtable field list record = { "Full Name": candidate_data.get("full_name"), "Email": candidate_data.get("email"), "Experience": candidate_data.get("total_years_experience"), "Job Role": job_role, "Match Score": candidate_data.get("job_fit_score"), "Resume Summary": candidate_data.get("hr_summary"), "Key Skills": skills_string, "LinkedIn": links.get("linkedin"), "Portfolio": links.get("portfolio"), "CV Link": candidate_data.get("cv_url"), "Raw JSON": json.dumps(candidate_data, indent=2) } # Push the record to Airtable response = table.create(record) return response except Exception as e: # If this fails, the error message will help us identify # any remaining field name mismatches. raise ValueError(f"Airtable Sync Error: {str(e)}")