Skip to content

Safe generation#239

Merged
battmanux merged 30 commits intomainfrom
safe_generation
Nov 24, 2025
Merged

Safe generation#239
battmanux merged 30 commits intomainfrom
safe_generation

Conversation

@battmanux
Copy link
Contributor

This pull request introduces several improvements and new features to the codebase, focusing on enhanced uncertainty handling, improvements to enum probability inspection, and new abstractions for guarded types. The changes also include utility and API enhancements for better developer ergonomics and more robust type handling.

Uncertainty Handling and Enum Probability Inspection:

  • Added new utilities and API functions for uncertainty estimation and reporting, including max_uncertainty, safe, and UncertaintyError, as well as print_last_probability_distribution and print_last_uncertainty for inspecting model output probabilities and uncertainties. (src/OpenHosta/__init__.py [1] [2] [3]; src/OpenHosta/core/logger.py [4]
  • Improved enum probability and uncertainty inspection in next/enum_probs.py, including a new example using a Year enum and demonstration of the new uncertainty utilities. (next/enum_probs.py next/enum_probs.pyL6-R103)

Type Handling and MetaPrompt Improvements:

  • Enhanced type conversion logic to better handle None as a valid return value in union types, including stricter checks and improved error messages. (src/OpenHosta/core/type_converter.py [1] [2]
  • Updated meta prompt templates and analyzers to conditionally allow "None" as a response when the return type permits it. (src/OpenHosta/core/meta_prompt.py [1]; src/OpenHosta/core/analizer.py [2]
  • Improved enum type description in prompt generation to clarify expected output format. (src/OpenHosta/core/type_converter.py src/OpenHosta/core/type_converter.pyL185-R204)

Guard and Guarded Types Abstraction:

  • Introduced a new Guarded abstract base class and related classes (GuardedInt, NonToxicComment, SafeComment, etc.) to enforce validation logic on value types, supporting composable and multi-guard scenarios. (next/guards_as_an_object.py next/guards_as_an_object.pyR133-R251)
  • Added example usage of dynamic function generation and execution using AST and emulate. (next/guards_as_an_object.py next/guards_as_an_object.pyR133-R251)

General Utility and Imports:

  • Added necessary imports and minor code cleanups to support new features and maintain consistency. (next/guards_as_an_object.py [1]; src/OpenHosta/core/analizer.py [2]; src/OpenHosta/core/type_converter.py [3]
  • Added TODO comments for future agent memory management features. (next/agents.py next/agents.pyR30-R39)

These changes collectively improve the framework's ability to reason about uncertainty, handle complex types, and enforce data validity, while also enhancing the developer experience.

- Updated test cases in `test_safe.py` to utilize the `safe` context manager for uncertainty control, replacing the previous decorator approach.
- Introduced a new `ReproducibleSettings` class and `ReproducibleContextManager` to manage uncertainty settings in a context-aware manner.
- Added a comprehensive set of error classes in `errors.py` to handle various exceptions related to requests and uncertainty.
- Implemented core uncertainty calculation functions in `uncertainty.py`, including methods for calculating certainty scores and posterior probabilities.
- Created a `max_uncertainty` decorator in `decorators.py` to allow users to configure uncertainty handling easily.
- Ensured that all relevant functions now properly track and report uncertainty, improving the robustness of the uncertainty estimation process.
@battmanux battmanux linked an issue Nov 23, 2025 that may be closed by this pull request
@battmanux battmanux merged commit 999a0d7 into main Nov 24, 2025
5 checks passed
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.

Documentation: explain . env file

1 participant