I’m facing a strange problem where OpenAI seems to be adding semicolons to my SQL queries when they’re processed via LangChain.
query = "DELETE FROM customers WHERE age > 65 DELETE FROM customers WHERE status = 'inactive';"
ai_model = ChatOpenAI(
model="gpt-3.5-turbo",
temperature=0.1,
verbose=False,
cache=True,
)
prompts = [
SystemMessage(
content=
"""
count the number of semicolons in this database query
"""
),
SystemMessage(content=query)
]
response = ai_model.invoke(prompts)
print(response.content)
My initial SQL query contains just one semicolon at the end, but after processing with OpenAI, it displays two. It looks like the AI is inserting an additional semicolon somewhere in my query.
Is there a way to identify the actual number of semicolons that I originally included in my SQL without the unintended additions?
Both answers above nailed the missing semicolon issue, but here’s something I’ve learned from debugging these problems.
I’ve hit this exact confusion before when expecting AI models to just count characters. The model’s actually being helpful - it sees your SQL syntax is broken and responds based on what valid SQL should look like.
Want to count actual semicolons without interpretation? Just use Python:
actual_count = query.count(';')
print(f"Actual semicolons in string: {actual_count}")
Honestly though, the AI’s doing you a solid by catching the syntax issue. I’ve debugged tons of SQL errors that started exactly like this - missing semicolons between statements.
Your query should be:
DELETE FROM customers WHERE age > 65; DELETE FROM customers WHERE status = 'inactive';
hey, openai isn’t adding semicolons! try running print(query.count(';')), you’ll find there’s only 1 in your string. the ai’s just indicating proper sql format - you need one between the delete statements, ya know?
Yeah, everyone caught the missing semicolon, but here’s what I’ve learned from dealing with LangChain quirks in production.
You’re asking an AI to count strings. That’s overkill - like using a sledgehammer on a nut. AI models interpret and fix things, they don’t count characters literally.
I see this constantly when people use LLMs for simple text tasks. You get weird behavior because the model wants to be helpful, not literal.
You need proper SQL validation and parsing automation. I’ve built systems that analyze SQL queries before they hit databases.
Skip LangChain for basic string ops. Build a workflow that handles SQL parsing right - parse the query, validate syntax, count characters, then use AI for complex stuff if needed.
I’ve solved this exact problem multiple times with automated workflows that split mechanical tasks from AI tasks. Way more reliable than hoping AI will count semicolons literally.
The issue isn’t OpenAI adding semicolons - your original query string is missing one. You’ve got two DELETE statements but no semicolon between them. It should be: “DELETE FROM customers WHERE age > 65; DELETE FROM customers WHERE status = ‘inactive’;” - see the semicolon after the first DELETE? When you ask the AI to count semicolons, it’s probably counting what should be there for proper SQL syntax (two semicolons total) rather than what’s literally in your string. Try using Python’s count method on your original string first to see the actual count before sending it to OpenAI.