Last active
December 20, 2025 16:59
-
-
Save Iamsdt/d0e036d400556f567a21b2a7f3113dfd to your computer and use it in GitHub Desktop.
TOON Format Benchmark: Efficient Structured Output with Gemini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from google.genai import types | |
| from google.genai import Client | |
| from dotenv import load_dotenv | |
| from toon_format import encode, decode | |
| load_dotenv() | |
| client = Client() | |
| def get_current_weather(location: str) -> str: | |
| """Returns the current weather and 15-day forecast. | |
| Args: | |
| location: The city and state, e.g. San Francisco, CA | |
| """ | |
| forecast = [] | |
| for i in range(16): # 0 for today, 1-15 for next 15 days | |
| day_data = { | |
| "date": f"2025-12-{21 + i:02d}", # Starting from Dec 21, 2025 | |
| "temperature": f"{70 + (i % 10)} F", # Varying temperatures | |
| "condition": ["sunny", "cloudy", "rainy", "partly cloudy"][i % 4], | |
| } | |
| forecast.append(day_data) | |
| data = { | |
| "location": location, | |
| "current": { | |
| "temperature": "72 F", | |
| "condition": "sunny", | |
| }, | |
| "forecast": forecast, | |
| } | |
| return encode(data) | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents="What is the weather like in New York? Share Next 15 days forecast as well.", | |
| config=types.GenerateContentConfig( | |
| tools=[get_current_weather], | |
| thinking_config=types.ThinkingConfig( | |
| include_thoughts=False, | |
| thinking_budget=0, | |
| ), | |
| ), | |
| ) | |
| print(response.text) | |
| print(response.usage_metadata) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "JD Index": 0, | |
| "Tokens Used": 1008, | |
| "Prompt Toknen": 639, | |
| "Completion Tokens": 369, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 1, | |
| "Tokens Used": 1191, | |
| "Prompt Toknen": 716, | |
| "Completion Tokens": 475, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 2, | |
| "Tokens Used": 1088, | |
| "Prompt Toknen": 726, | |
| "Completion Tokens": 362, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 3, | |
| "Tokens Used": 1002, | |
| "Prompt Toknen": 646, | |
| "Completion Tokens": 356, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 4, | |
| "Tokens Used": 1065, | |
| "Prompt Toknen": 648, | |
| "Completion Tokens": 417, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 5, | |
| "Tokens Used": 1034, | |
| "Prompt Toknen": 635, | |
| "Completion Tokens": 399, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 6, | |
| "Tokens Used": 1137, | |
| "Prompt Toknen": 688, | |
| "Completion Tokens": 449, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 7, | |
| "Tokens Used": 964, | |
| "Prompt Toknen": 593, | |
| "Completion Tokens": 371, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 8, | |
| "Tokens Used": 956, | |
| "Prompt Toknen": 594, | |
| "Completion Tokens": 362, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 9, | |
| "Tokens Used": 872, | |
| "Prompt Toknen": 584, | |
| "Completion Tokens": 288, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "JD Index": 0, | |
| "Tokens Used": 1200, | |
| "Prompt Toknen": 794, | |
| "Completion Tokens": 406, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 1, | |
| "Tokens Used": 1353, | |
| "Prompt Toknen": 871, | |
| "Completion Tokens": 482, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 2, | |
| "Tokens Used": 1291, | |
| "Prompt Toknen": 881, | |
| "Completion Tokens": 410, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 3, | |
| "Tokens Used": 1243, | |
| "Prompt Toknen": 801, | |
| "Completion Tokens": 442, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 4, | |
| "Tokens Used": 1188, | |
| "Prompt Toknen": 803, | |
| "Completion Tokens": 385, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 5, | |
| "Tokens Used": 1364, | |
| "Prompt Toknen": 790, | |
| "Completion Tokens": 574, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 6, | |
| "Tokens Used": 1276, | |
| "Prompt Toknen": 843, | |
| "Completion Tokens": 433, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 7, | |
| "Tokens Used": 1282, | |
| "Prompt Toknen": 748, | |
| "Completion Tokens": 534, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 8, | |
| "Tokens Used": 1182, | |
| "Prompt Toknen": 749, | |
| "Completion Tokens": 433, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 9, | |
| "Tokens Used": 1194, | |
| "Prompt Toknen": 739, | |
| "Completion Tokens": 455, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "JD Index": 0, | |
| "Tokens Used": 1136, | |
| "Prompt Toknen": 715, | |
| "Completion Tokens": 421, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 1, | |
| "Tokens Used": 1278, | |
| "Prompt Toknen": 792, | |
| "Completion Tokens": 486, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 2, | |
| "Tokens Used": 1277, | |
| "Prompt Toknen": 802, | |
| "Completion Tokens": 475, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 3, | |
| "Tokens Used": 1284, | |
| "Prompt Toknen": 722, | |
| "Completion Tokens": 562, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 4, | |
| "Tokens Used": 1159, | |
| "Prompt Toknen": 724, | |
| "Completion Tokens": 435, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 5, | |
| "Tokens Used": 1236, | |
| "Prompt Toknen": 711, | |
| "Completion Tokens": 525, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 6, | |
| "Tokens Used": 1199, | |
| "Prompt Toknen": 764, | |
| "Completion Tokens": 435, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 7, | |
| "Tokens Used": 1113, | |
| "Prompt Toknen": 669, | |
| "Completion Tokens": 444, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 8, | |
| "Tokens Used": 1151, | |
| "Prompt Toknen": 670, | |
| "Completion Tokens": 481, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 9, | |
| "Tokens Used": 1101, | |
| "Prompt Toknen": 660, | |
| "Completion Tokens": 441, | |
| "Parsing Status": "Success", | |
| "method": "json" | |
| }, | |
| { | |
| "JD Index": 0, | |
| "Tokens Used": 907, | |
| "Prompt Toknen": 721, | |
| "Completion Tokens": 186, | |
| "Parsing Status": "Failed", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 1, | |
| "Tokens Used": 938, | |
| "Prompt Toknen": 798, | |
| "Completion Tokens": 140, | |
| "Parsing Status": "Failed", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 2, | |
| "Tokens Used": 1056, | |
| "Prompt Toknen": 808, | |
| "Completion Tokens": 248, | |
| "Parsing Status": "Failed", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 3, | |
| "Tokens Used": 1084, | |
| "Prompt Toknen": 728, | |
| "Completion Tokens": 356, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 4, | |
| "Tokens Used": 881, | |
| "Prompt Toknen": 730, | |
| "Completion Tokens": 151, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 5, | |
| "Tokens Used": 994, | |
| "Prompt Toknen": 717, | |
| "Completion Tokens": 277, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 6, | |
| "Tokens Used": 1083, | |
| "Prompt Toknen": 770, | |
| "Completion Tokens": 313, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 7, | |
| "Tokens Used": 914, | |
| "Prompt Toknen": 675, | |
| "Completion Tokens": 239, | |
| "Parsing Status": "Failed", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 8, | |
| "Tokens Used": 887, | |
| "Prompt Toknen": 676, | |
| "Completion Tokens": 211, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| }, | |
| { | |
| "JD Index": 9, | |
| "Tokens Used": 815, | |
| "Prompt Toknen": 666, | |
| "Completion Tokens": 149, | |
| "Parsing Status": "Success", | |
| "method": "toon" | |
| } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from google.genai import Client | |
| from dotenv import load_dotenv | |
| from pydantic import BaseModel | |
| from toon_format import encode, decode | |
| load_dotenv() | |
| class JobModel(BaseModel): | |
| Role: str | |
| Primary_Skills: list[str] | |
| Secondary_Skills: list[str] | |
| Minimum_Experience: str | |
| Maximum_Experience: str | |
| Location: str | |
| Employment_Type: str | |
| Summary: str | |
| Responsibilities: list[str] | |
| client = Client() | |
| with open("jds.json") as f: | |
| jds = json.load(f) | |
| stats = [] | |
| def check_json(jds): | |
| for i, v in enumerate(jds): | |
| prompt = f"""Extract Role, Primary Skills, Secondary Skills, | |
| Minimum Experience, Maximum Experience, | |
| Location, Employment Type, Summary, and Responsibilities | |
| from the following job description in JSON format. | |
| Job Description: | |
| ``` | |
| {v} | |
| ``` | |
| """ | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| config={ | |
| "thinking_config": { | |
| "include_thoughts": False, | |
| "thinking_budget": 0, | |
| }, | |
| "response_mime_type": "application/json", | |
| "response_schema": JobModel, | |
| }, | |
| ) | |
| print(f"JD#{i}", "Tokens used:", response.usage_metadata.total_token_count) | |
| text = response.text or "" | |
| try: | |
| if not response.parsed: | |
| raise json.JSONDecodeError("No parsed data", text, 0) | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Success", | |
| "method": "json", | |
| } | |
| ) | |
| except json.JSONDecodeError: | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Failed", | |
| "method": "json", | |
| } | |
| ) | |
| check_json(jds) | |
| print("done") | |
| with open("jd_extractions-pydantic-flash-2.5.json", "w") as f: | |
| json.dump(stats, f, indent=4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from google.genai import Client | |
| from dotenv import load_dotenv | |
| from toon_format import encode, decode | |
| load_dotenv() | |
| client = Client() | |
| with open("jds.json") as f: | |
| jds = json.load(f) | |
| stats = [] | |
| def check_json(jds): | |
| for i, v in enumerate(jds): | |
| prompt = f"""Extract Role, Primary Skills, Secondary Skills, | |
| Minimum Experience, Maximum Experience, | |
| Location, Employment Type, Summary, and Responsibilities | |
| from the following job description in JSON format. | |
| Job Description: | |
| ``` | |
| {v} | |
| ``` | |
| JSON Output: | |
| ``` | |
| {{ | |
| "Role": "", | |
| "Primary Skills": [], | |
| "Secondary Skills": [], | |
| "Minimum Experience": "", | |
| "Maximum Experience": "", | |
| "Location": "", | |
| "Employment Type": "", | |
| "Summary": "", | |
| "Responsibilities": [] | |
| }} | |
| ``` | |
| """ | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| config={ | |
| "thinking_config": { | |
| "include_thoughts": False, | |
| "thinking_budget": 0, | |
| } | |
| }, | |
| ) | |
| print(f"JD#{i}", "Tokens used:", response.usage_metadata.total_token_count) | |
| text = response.text or "" | |
| try: | |
| text = text.replace("```", "").strip() | |
| text = text.replace("json", "").strip() | |
| data = json.loads(text) | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Success", | |
| "method": "json", | |
| } | |
| ) | |
| except json.JSONDecodeError: | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Failed", | |
| "method": "json", | |
| } | |
| ) | |
| def check_toon(jds): | |
| for i, v in enumerate(jds): | |
| prompt = f"""Extract Role, Primary Skills, Secondary Skills, | |
| Minimum Experience, Maximum Experience, | |
| Location, Employment Type, Summary, and Responsibilities | |
| from the following job description in TOON format. | |
| Job Description: | |
| ``` | |
| {v} | |
| ``` | |
| TOON Output: | |
| ``` | |
| Role: "" | |
| "Primary Skills"[2]: Python,JavaScript | |
| "Secondary Skills"[2]: Responsibility,Communication | |
| "Minimum Experience": "" | |
| "Maximum Experience": "" | |
| Location: "" | |
| "Employment Type": "" | |
| Summary: "" | |
| Responsibilities[2]: Task A,Task B | |
| ``` | |
| """ | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| config={ | |
| "thinking_config": { | |
| "include_thoughts": False, | |
| "thinking_budget": 0, | |
| } | |
| }, | |
| ) | |
| print(f"JD#{i}", "Tokens used:", response.usage_metadata.total_token_count) | |
| text = response.text or "" | |
| try: | |
| text = text.replace("```", "").strip() | |
| data = decode(text) | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Success", | |
| "method": "toon", | |
| } | |
| ) | |
| except Exception as e: | |
| print(f"Error parsing TOON format for JD#{i}: {e}") | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Failed", | |
| "method": "toon", | |
| } | |
| ) | |
| check_json(jds) | |
| check_toon(jds) | |
| print("done") | |
| with open("jd_extractions.json", "w") as f: | |
| json.dump(stats, f, indent=4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from google.genai import Client | |
| from dotenv import load_dotenv | |
| from toon_format import encode, decode | |
| load_dotenv() | |
| client = Client() | |
| with open("jds.json") as f: | |
| jds = json.load(f) | |
| stats = [] | |
| def check_toon(jds): | |
| for i, v in enumerate(jds): | |
| prompt = f""" | |
| Extract Role, Primary_Skills, Secondary_Skills, | |
| Minimum_Experience, Maximum_Experience, | |
| Location, Employment_Type, Summary, and Responsibilities | |
| Job Description: | |
| ``` | |
| {v} | |
| ``` | |
| Output in TOON format. Example structure: | |
| ``` | |
| Role: "Senior Data Scientist" | |
| Primary_Skills: | |
| [0]: "Python" | |
| [1]: "Machine Learning" | |
| [2]: "Statistical Analysis" | |
| Secondary_Skills: | |
| [0]: "Big Data" | |
| [1]: "Cloud Platforms" | |
| Minimum_Experience: "5 years" | |
| Maximum_Experience: "10 years" | |
| Location: "New York, NY or Remote" | |
| Employment_Type: "Full-time" | |
| Summary: "Lead data science initiatives" | |
| Responsibilities: | |
| [0]: "Design ML models" | |
| [1]: "Analyze datasets" | |
| ``` | |
| Now provide the extraction in TOON format. Keep the format exactly as shown above. | |
| """ | |
| response = client.models.generate_content( | |
| model="gemini-2.5-flash", | |
| contents=prompt, | |
| config={ | |
| "thinking_config": { | |
| "include_thoughts": False, | |
| "thinking_budget": 0, | |
| } | |
| }, | |
| ) | |
| print(f"JD#{i}", "Tokens used:", response.usage_metadata.total_token_count) | |
| text = response.text or "" | |
| try: | |
| text = text.replace("```", "").strip() | |
| print("TOON Response:") | |
| data = decode(text) | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Success", | |
| "method": "toon", | |
| } | |
| ) | |
| except Exception as e: | |
| print(f"Error parsing TOON format for JD#{i}: {e}") | |
| stats.append( | |
| { | |
| "JD Index": i, | |
| "Tokens Used": response.usage_metadata.total_token_count, | |
| "Prompt Toknen": response.usage_metadata.prompt_token_count, | |
| "Completion Tokens": response.usage_metadata.candidates_token_count, | |
| "Parsing Status": "Failed", | |
| "method": "toon", | |
| } | |
| ) | |
| check_toon(jds) | |
| print("done") | |
| with open("jd_extractions-toon.json", "w") as f: | |
| json.dump(stats, f, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment