How to catch "runs fine, physics is wrong" bugs in LLM-written code?

13 replies · 718 views · last reply
YihangLyu
Messages
5
Reaction score
3
TL;DR
LLM scripts often run cleanly while getting the physics wrong (examples attached from a quick ERA5 check). What simple sanity checks do you use?
Hi everyone,

I'm a first-year grad student in atmospheric science (mostly extreme precipitation and satellite data). When I use LLMs to help write Python analysis scripts, the annoying part isn't syntax errors. The code usually runs fine and gives numbers that look normal, until you notice the physics or geometry is wrong.

I plotted two quick examples using an ERA5 precip file I had on hand (attached). In (a), the script forgot cos(lat) when taking the global mean, so it gave 2.39 mm/day instead of 2.91 mm/day (about 18% off). In (b), it used tp > 0 for wet days and flagged 89% of the globe as rainy, instead of a standard 1.0 mm/day threshold (42%), and ignored tiny negative values in the dataset.

I want to put a few simple checks in my workflow before trusting any script output (units, area weights, non-negative bounds, etc.). If you write scientific code regularly, what basic sanity checks do you usually include to catch silent errors like this?
era5_sanity_checks_pf.webp
 
Last edited by a moderator:
  • Like
Likes   Reactions: AlexB23 and Matterwave
After dealing with a problematic reply (now deleted), the thread is reopened. Thanks for your patience.
 
  • Like
Likes   Reactions: YihangLyu and Matterwave
I'm not familiar with your field so I can only give general advice.

1. Before you ask the AI to build the script it might be worthwhile to do a mini design session with it. If you're using Claude Code or GPT Codex you should be able to do a planning session where Claude comes up with a plan that you can review. Having a nice plan helps prevent the AI from missing things.
2. Ignoring the AI, how would you have caught these issues if you or another student were the one writing the code? Would it be unit tests? Evaluations? Whatever that is, you should implement the same thing for AI generated code.

It seems to me that the issue here is only tangentially AI related. The core issue is just basic software principles.
 
  • Like
Likes   Reactions: YihangLyu, berkeman and FactChecker
IMO, this shows a bad attitude toward code verification/validation. If you are going to rely on that code in any serious way, you should do much more than a "sanity check". Auto-generated code (in fact, any automated computer process) should be verified with a thorough set of test cases.
 
  • Like
Likes   Reactions: Filip Larsen, YihangLyu, berkeman and 1 other person
LLM is a "yes man", so you can use an LLM to "confirm" how bad the result provided by another LLM is.
"You are an expert in X, check this document that I think is wrong": the LLM will try to prove that the document is wrong.
You can use same LLM as long as you start from "fresh history" and no previous context.
 
  • Like
Likes   Reactions: YihangLyu and FactChecker
I'm sorry?

How do you avoid these issues? Well... you read the code it spit out and check it for accuracy. Any time you publish in a serious journal where you're relying on simulations or complex analysis scripts they're going to ask that you have the code used to generate paper results on git. It should be treated with the same care you apply to anything else in the scientific process.
 
Thanks, this is helpful. I do think the physical conditions and bounds have to be pinned down before an AI writes scientific code, and I wouldn't skip that. I'm less sure this is just a software-testing problem, though. In atmospheric science we often don't even know if a particular direction is right, so I'm not always confident the test cases I write are themselves correct. How do you build a thorough set of tests when the right answer is still something you're trying to find out?
Matterwave said:
I'm not familiar with your field so I can only give general advice.

1. Before you ask the AI to build the script it might be worthwhile to do a mini design session with it. If you're using Claude Code or GPT Codex you should be able to do a planning session where Claude comes up with a plan that you can review. Having a nice plan helps prevent the AI from missing things.
2. Ignoring the AI, how would you have caught these issues if you or another student were the one writing the code? Would it be unit tests? Evaluations? Whatever that is, you should implement the same thing for AI generated code.

It seems to me that the issue here is only tangentially AI related. The core issue is just basic software principles.


I've been thinking about this. In physics, especially in atmospheric science, which is my field, we often don't even know whether a particular direction is correct; we have to rely on intuition and then explore. That is different from pure computer science.

I've also been thinking about how to create effective test cases for verification. But even after writing them, I'm not really sure how much they actually help an AI understand the physics. In a field like ours, which must be falsifiable, I also can't be sure that the test cases I write are themselves correct.

Even so, I still believe it is necessary to impose appropriate and reasonable physical constraints. When AI is used in scientific research, it may be more a tool for exploration.
FactChecker said:
IMO, this shows a bad attitude toward code verification/validation. If you are going to rely on that code in any serious way, you should do much more than a "sanity check". Auto-generated code (in fact, any automated computer process) should be verified with a thorough set of test cases.


The yes-man problem is a good point too. A fresh-session review might catch some mistakes, but if I don't hint that something is wrong, the model may just go along with a plausible result; if I do, it may try to prove it's wrong. So I wouldn't treat that as real verification.
Roberto Pavani said:
LLM is a "yes man", so you can use an LLM to "confirm" how bad the result provided by another LLM is.
"You are an expert in X, check this document that I think is wrong": the LLM will try to prove that the document is wrong.
You can use same LLM as long as you start from "fresh history" and no previous context.
 
I strongly agree that the code has to be checked carefully against the physical conditions, just as we would with code written by a person. In practice, though, a research project often involves a large amount of code, and it's not realistic to inspect every line with that level of rigor. It takes a lot of time and effort, and even then you may still miss something. I think we still need AI to help with scientific work. That's also why the earlier suggestion about building a good set of test cases seems worth discussing.
QuarkyMeson said:
I'm sorry?

How do you avoid these issues? Well... you read the code it spit out and check it for accuracy. Any time you publish in a serious journal where you're relying on simulations or complex analysis scripts they're going to ask that you have the code used to generate paper results on git. It should be treated with the same care you apply to anything else in the scientific process.
 
YihangLyu said:
How do you build a thorough set of tests when the right answer is still something you're trying to find out?
This is not any different of a problem with or without AI. How do you verify that code you wrote is good or not?

YihangLyu said:
I've also been thinking about how to create effective test cases for verification. But even after writing them, I'm not really sure how much they actually help an AI understand the physics.
Don't give the AI your test cases. You are just teaching it how to game the system ("reward hack" in technical language). You can give it examples, but you don't show it exactly how it will be evaluated. Otherwise your test cases won't be useful.

YihangLyu said:
In a field like ours, which must be falsifiable, I also can't be sure that the test cases I write are themselves correct.
You write tests for which you know the correct answer.
 
  • Like
Likes   Reactions: YihangLyu and FactChecker
YihangLyu said:
I strongly agree that the code has to be checked carefully against the physical conditions, just as we would with code written by a person. In practice, though, a research project often involves a large amount of code, and it's not realistic to inspect every line with that level of rigor. It takes a lot of time and effort, and even then you may still miss something. I think we still need AI to help with scientific work. That's also why the earlier suggestion about building a good set of test cases seems worth discussing.
But it is, and it has to be done that way.

A research project might also involve forty or fifty direct references to prior work and each of those also needs to checked manually and the references they're directly citing, and so on. You literally need to go through them with a fine tooth comb. That's science man.

It's the same with code, you need to understand what each line of your code is doing, why it's doing it, what the libraries and built in functions you're calling do, etc.

You don't need AI to help with scientific work. Works were published and done without AI since we basically started systematic science in the 1500's. That doesn't mean you should throw out a tool. AI has it's uses, but vibe coding science analysis isn't one of them.

You can certainty probably get by for a time not doing this, but you're going to get burnt eventually. There is no fool proof set of unit tests you could perform that would guarantee accuracy without reading your own code. That last statement might seem absurd, and it was absurd just a few years ago.

Also, I just wanted to mention on the topics of errors. Almost every work you read will have errors, generally, if peer review did it's job the errors are mostly tangential or unrelated to the core of the work, which is the point. Not that it's 100% error free.
 
Last edited:
That's a really good suggestion. I've already gotten some ideas from it, and I'll try it out soon.
Matterwave said:
This is not any different of a problem with or without AI. How do you verify that code you wrote is good or not?


Don't give the AI your test cases. You are just teaching it how to game the system ("reward hack" in technical language). You can give it examples, but you don't show it exactly how it will be evaluated. Otherwise your test cases won't be useful.


You write tests for which you know the correct answer.
 
YihangLyu said:
In atmospheric science we often don't even know if a particular direction is right, so I'm not always confident the test cases I write are themselves correct. How do you build a thorough set of tests when the right answer is still something you're trying to find out?
That's a good question.

Matterwave said:
You write tests for which you know the correct answer.
I agree. They don't even have to be a real-world situation, They can be the extreme cases where the correct answer is relatively clear, even if it is an unrealistic case.
 
QuarkyMeson said:
But it is, and it has to be done that way.

A research project might also involve forty or fifty direct references to prior work and each of those also needs to checked manually and the references they're directly citing, and so on. You literally need to go through them with a fine tooth comb. That's science man.

It's the same with code, you need to understand what each line of your code is doing, why it's doing it, what the libraries and built in functions you're calling do, etc.

You don't need AI to help with scientific work. Works were published and done without AI since we basically started systematic science in the 1500's. That doesn't mean you should throw out a tool. AI has it's uses, but vibe coding science analysis isn't one of them.

You can certainty probably get by for a time not doing this, but you're going to get burnt eventually. There is no fool proof set of unit tests you could perform that would guarantee accuracy without reading your own code. That last statement might seem absurd, and it was absurd just a few years ago.

Also, I just wanted to mention on the topics of errors. Almost every work you read will have errors, generally, if peer review did it's job the errors are mostly tangential or unrelated to the core of the work, which is the point. Not that it's 100% error free.
I think what you said makes sense. Thanks for the insight.