Skip to content

fix: gracefully handle hallucinated dataset IDs in populate_stage to prevent KeyError #152#155

Open
veryniceuser wants to merge 8 commits intodevelopmentfrom
fix/keyerror-hallucinated-dataset-id-in-populate-stage
Open

fix: gracefully handle hallucinated dataset IDs in populate_stage to prevent KeyError #152#155
veryniceuser wants to merge 8 commits intodevelopmentfrom
fix/keyerror-hallucinated-dataset-id-in-populate-stage

Conversation

@veryniceuser
Copy link
Contributor

@veryniceuser veryniceuser commented Feb 18, 2026

Skip unknown dataset IDs with a warning log in format_multidataset_queries and V2 populate_stage dataset references, instead of crashing. This prevents a KeyError when the LLM hallucinates a dataset UUID before _remove_hallucinations gets a chance to filter it out.

Applicable issues

Description of changes

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

…Error

Skip unknown dataset IDs with a warning log in format_multidataset_queries
and V2 populate_stage dataset references, instead of crashing. This prevents
a KeyError when the LLM hallucinates a dataset UUID before _remove_hallucinations
gets a chance to filter it out.
@veryniceuser veryniceuser requested a review from ypldan as a code owner February 18, 2026 13:24
@veryniceuser veryniceuser self-assigned this Feb 18, 2026
@navalnica
Copy link
Contributor

the issue is not only with formatting. if llm selected incorrect uuid, the whole query is unsable. we need to delete it inside indicator selection chain

@navalnica
Copy link
Contributor

also, it's better not to pass uuid to llm. instead, we can pass:

  • dataset urn (imf:weo, for example)
  • source_id/entity_id
  • 1-based index

we also need to make sure there are no duplicates. for example if there are duplicated urns (for some reason. though they should not) we can add index prefix: "1-imf:weo"

@veryniceuser
Copy link
Contributor Author

the issue is not only with formatting. if llm selected incorrect uuid, the whole query is unsable. we need to delete it inside indicator selection chain

I think it gets deleted in the latter stage at _remove_hallucinations()

@veryniceuser
Copy link
Contributor Author

also, it's better not to pass uuid to llm. instead, we can pass:

  • dataset urn (imf:weo, for example)
  • source_id/entity_id
  • 1-based index

we also need to make sure there are no duplicates. for example if there are duplicated urns (for some reason. though they should not) we can add index prefix: "1-imf:weo"

Sound like a bigger change. Should we merge this PR as a hotfix, and create a follow-up feature request to make these changes?

@navalnica
Copy link
Contributor

I don't like the current proposed change. it states that, when populating stage or formatting query, the query can contain dataset queries to unavailable datasets. this seems incorrect - we should remove any hallucinations from LLM response and call formatting only afterwards

@veryniceuser veryniceuser changed the title fix: handle hallucinated dataset IDs in populate_stage to prevent KeyError #152 hotfix: gracefully handle hallucinated dataset IDs in populate_stage to prevent KeyError #152 Feb 27, 2026
@veryniceuser
Copy link
Contributor Author

also, it's better not to pass uuid to llm. instead, we can pass:

  • dataset urn (imf:weo, for example)
  • source_id/entity_id
  • 1-based index

we also need to make sure there are no duplicates. for example if there are duplicated urns (for some reason. though they should not) we can add index prefix: "1-imf:weo"

Created a separate feature request: #172, and pushed changes in PR #173. It's done to un-block this hotfix merge, and to have review and discussion for the in-depth fix in the separate place.

@veryniceuser
Copy link
Contributor Author

veryniceuser commented Feb 27, 2026

I don't like the current proposed change. it states that, when populating stage or formatting query, the query can contain dataset queries to unavailable datasets. this seems incorrect - we should remove any hallucinations from LLM response and call formatting only afterwards

I disagree that proposed change is wrong, populate stage should not make implicit assumptions, and must not fail if the code that calls it will change in the future.

On the other hand, swapping remove_hallucinations and populate_stage makes sense. But because of the code structure, such change seem to require refactoring. And so should be prioritized separately

@veryniceuser veryniceuser removed the request for review from ypldan February 27, 2026 09:36
Add warning log for unknown dataset IDs when populating stage content.
@veryniceuser veryniceuser changed the title hotfix: gracefully handle hallucinated dataset IDs in populate_stage to prevent KeyError #152 fix: gracefully handle hallucinated dataset IDs in populate_stage to prevent KeyError #152 Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError in populate_stage when LLM hallucinates dataset ID

3 participants