Gemini, SymPy & Me: The Toxic Love Triangle
Teaching AI to Respect Mathematics
Gemini, SymPy & Me: The Toxic Love Triangle
Teaching AI to Respect Mathematics
AI seems magical when you’re building small, fun projects. A chatbot here. A to-do app there. Everything works. Demos shine. Your ego inflates.
Then January arrived. Along with that dangerous “new year, new life, I will conquer the world” motivation. Not the world, actually, Just… Gemini.
Happy and excited,
I had just started my internship, and my main system was assigned to me. it looked simple. I did what every half-confident developer does. I was like,
**“Hmm…, Easy. I know LLMs, I have coding tools. Give me two days. you just watch”,** (Nobody was watching. But my ego was).
I was wrong. It didn’t just break my code; it broke my spirit. It changed my mental health for two weeks and left me debugging JSON at 12:30 AM while questioning every career choice I’d ever made. This is the story of how Gemini, SymPy, and I entered a toxic love triangle.
1. The Cast: A Poet, a Pedant, and a Student
To build a system that actually respects math, I couldn’t rely on one model. I had to build a “Neural-Symbolic” hybrid system, essentially giving a creative brain a calculator it wasn’t allowed to ignore.
- Gemini 2.5 Flash (The Intuition): I started with 1.5, but, the universe (and Google) deprecated it. I moved to gemini-2.5-Flash. It’s the “Poet”, brilliant at reading a student’s messy handwriting or vague word problems, but statistically prone to “feeling” that x+5=10 means x=2, if it’s having a bad day.
- SymPy (The Pedant): A Python library that represents mathematical truth. It doesn’t guess. It doesn’t have “vibes.” If you give it a typo, it crashes. It was my emotional support library because it was the only thing in the system that never lied to me.
- Me (The Architect/Victim): The person trying to make them talk to each other without the whole thing exploding. hehe …
This isn’t just a blog post; it’s a post-mortem of a mental health crisis disguised as a project.
2. The Villain: The “Backtick Betrayal”
The most traumatizing part of this wasn’t the math, it was the handshake.
I told Gemini:
“Output ONLY raw JSON.”
Gemini replied:
“Sure! Here is the JSON you requested { … } Hope this helps!”
and then wrapped it in Markdown backticks (json ...).
My Python parser would hit that first backtick and immediately throw a *JSONDecodeError*. At 12:30 AM, looking at a screen of red text, you begin to suspect the model is laughing at you for not trusting it.
The Fix: The Sanitizer
At 12:45 AM, tired, stubborn, and mildly questioning my existence, I chose violence, against Markdown. To break the model’s ego, I wrote a small “cleanser” to strip away its artistic flourishes and return only what mattered: valid JSON.
import re
def sanitize_llm_json(raw_text: str) -> str:
"""The 'Handshake' logic: Strips AI fluff to get to the data."""
# Find content between ```json and ```
match = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", raw_text, re.DOTALL)
if match:
return match.group(1).strip()
# Fallback: Find the first '{' and last '}'
try:
start = raw_text.index('{')
end = raw_text.rindex('}') + 1
return raw_text[start:end].strip()
except ValueError:
return "{}"
3. Why Not Just Gemini?
Someone always asks:
“Why use SymPy? Gemini is smart enough to solve x + 2 = 4.”
Sure, for x + 2 = 4. But what about x^(2.5) + log(y) = 14 ?
LLMs are probabilistic. They predict the next most likely word. Math is deterministic. When Gemini solves math, it’s like a novelist trying to calculate the structural integrity of a bridge. It sounds beautiful, but the bridge might still fall down.
SymPy is the anchor. It provides a Ground Truth that the LLM cannot fabricate.
4. The “Takeover” (When the Calculator Fails)
The real “aha!” moment came when I realized the system needed a Logic Fallback. If a student asks,
“Which is heavier: a kg of lead or a kg of feathers?”
SymPy will crash because it can’t find an equation for the “feathers.”
We built a Takeover Mechanism:
- Phase 1: Gemini tries to extract equations.
- Phase 2: SymPy tries to solve.
- Phase 3 (The Takeover): If SymPy hits a wall, we hand the mic back to Gemini for pure linguistic reasoning.
try:
solution = sympy_solve(equations)
final_output = gemini.explain(solution)
except Exception:
# The 'Fluid' Path: Logic over Math
final_output = gemini.fallback_reasoning(problem_input)
5. The Final Boss: The Bill
Everything worked. The UI was beautiful. The logic was sound. Then, the universe spoke again,
“Welcome to the “I finally built something and the universe said: pay.” club.”
And it didn’t whisper. It threw this:
google.genai.errors.ClientError: 429 RESOURCE_EXHAUSTED.
{'error': {'code': 429, 'message': 'You exceeded your current quota, please
check your plan and billing details. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits.
To monitor your current usage, head to: https://ai.dev/rate-limit.
\n* Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_free_tier_input_token_count,
limit: 0, model: gemini-2.0-flash-lite\n*}
Translation:
“Congratulations on your working system. Now pay me.”
“Exit code: 429. Ego: terminated.”
Reality check. Cloud LLMs are not API’s. I was building a distributed token-burning machine.
I had unknowingly built an architecture where:
- every user query
- every equation extraction
- every explanation
- every retry
- every mistake
hit a cloud model. And my wallet is not designed to fight Google’s rate limiter.
The New Rule
Local models for survival. Cloud models for intelligence.(Ollama will meet me soon.) (But for now, System status: low battery. Initiating Nezuko Kamado sleep mode. Demon.exe not found.)
Closing: The “Broken but Beautiful” Philosophy
Two weeks later, I didn’t have a perfect system. I had an honest one. A system that tells you
“I don’t know”
is infinitely more valuable than one that lies to you with a smile.
your system design matters more than your model choice because models change, APIs break, pricing shifts, and quotas expire… but fragile architecture hurts forever.
I started & here I’m with a modular, intentional mess that I am incredibly proud of. Because in the real world, the “magic” isn’t in the model, It’s in the person stubborn enough to debug, research and make things work.
And you are always more magic than you realize.
Happy Debugging ! Happy Researching !
A question for you:
What mistake did you make in your first “real” system that tutorials never warned you about?
메타데이터
- post_id
- 28eec3eb2c03
- slug
- gemini-sympy-me-the-toxic-love-triangle-28eec3eb2c03
- url
- https://medium.com/@ghadgemadhuri92/gemini-sympy-me-the-toxic-love-triangle-28eec3eb2c03
- canonical_url
- https://medium.com/@ghadgemadhuri92/gemini-sympy-me-the-toxic-love-triangle-28eec3eb2c03
- author_url
- https://medium.com/@ghadgemadhuri92
- status
- ok
- fetched_at
- 2026-07-13 11:44:12