API Reference
Protocols
Section titled “Protocols”FeedbackProtocol
Section titled “FeedbackProtocol”Protocol for submitting and querying AI feedback.
Submit feedback for an AI generation, scoped to an owner.
| Parameter | Type | Description |
|---|---|---|
| `trace_id` | str | Identifier of the AI generation trace. |
| `score` | float | Numeric feedback score (e.g. 0.0-1.0 or 1-5). |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `comment` | str | None | Optional free-text comment stored in metadata. |
| `metadata` | dict[str, Any] | None | Optional additional key-value metadata. |
Query aggregate feedback statistics for an owner.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are aggregated. |
| `model` | str | None | Optional model name for context (currently informational). |
| `provider` | str | None | Optional provider name for context (currently informational). |
FeedbackStoreProtocol
Section titled “FeedbackStoreProtocol”Persist and query collected feedback items.
All implementations must be async and treat save() as safe to call from request-handling hot-paths (synchronous returns acceptable for cache misses).
Persist a single feedback item.
| Parameter | Type | Description |
|---|---|---|
| `feedback` | FeedbackItem | The feedback item to store. |
| Type | Description |
|---|---|
| Result[str, Exception] | Ok(feedback.id) on success, Err(exception) on failure. |
Retrieve all feedback items for a session, scoped to an owner.
| Parameter | Type | Description |
|---|---|---|
| `session_id` | str | Session identifier from feedback context. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| Type | Description |
|---|---|
| list[FeedbackItem] | All collected items in that session, newest first. |
Retrieve feedback items of a given type, scoped to an owner.
| Parameter | Type | Description |
|---|---|---|
| `feedback_type` | FeedbackType | The type to filter by. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| `limit` | int | Maximum number of results (default 100). |
| Type | Description |
|---|---|
| list[FeedbackItem] | Matching items ordered by creation time descending. |
Compute summary statistics for an owner’s items in a time window.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are aggregated. |
| `window_hours` | int | Look-back window in hours (default 24). |
| Type | Description |
|---|---|
| FeedbackSummary | Aggregated FeedbackSummary statistics. |
Classes
Section titled “Classes”CachedFeedbackStore
Section titled “CachedFeedbackStore”Write-through cache: cold reads from *store*, hot reads from *cache*.
Session-scoped and type-scoped result lists are cached with short TTLs. Any save call invalidates the relevant cache entries so subsequent reads always reflect the latest data.
| Parameter | Type | Description |
|---|---|---|
| `store` | Durable backing store (e.g. DatabaseFeedbackStore). | |
| `cache` | Cache backend used for hot reads. |
Persist feedback and invalidate related cache entries.
| Parameter | Type | Description |
|---|---|---|
| `feedback` | FeedbackItem | Item to persist. |
| Type | Description |
|---|---|
| Result[str, Exception] | Result from the backing store. |
Return items for session_id owned by owner_id, using cache when available.
| Parameter | Type | Description |
|---|---|---|
| `session_id` | str | Session identifier. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| Type | Description |
|---|---|
| list[FeedbackItem] | Feedback items for the session, newest first. |
Return items of feedback_type owned by owner_id, using cache when available.
| Parameter | Type | Description |
|---|---|---|
| `feedback_type` | FeedbackType | Type to filter by. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| `limit` | int | Maximum result count. |
| Type | Description |
|---|---|
| list[FeedbackItem] | Matching feedback items, newest first. |
Delegate aggregation to the backing store — not cached.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are aggregated. |
| `window_hours` | int | Look-back window in hours. |
| Type | Description |
|---|---|
| FeedbackSummary | FeedbackSummary. |
DatabaseFeedbackStore
Section titled “DatabaseFeedbackStore”Persist and query feedback items via DatabaseProviderProtocol.
The backing table (ai_feedback) is created lazily on first write.
Indexed columns: session_id, type, created_at.
| Parameter | Type | Description |
|---|---|---|
| `provider` | DI-injected database provider. |
Persist feedback and return its ID on success.
| Parameter | Type | Description |
|---|---|---|
| `feedback` | FeedbackItem | Item to store. |
| Type | Description |
|---|---|
| Result[str, FeedbackError] | ``Ok(feedback.id)`` on success, ``Err(FeedbackError)`` on failure. |
Return all items collected during session_id for owner_id, newest first.
| Parameter | Type | Description |
|---|---|---|
| `session_id` | str | Session identifier to filter by. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| Type | Description |
|---|---|
| list[FeedbackItem] | Matching feedback items ordered by creation time descending. |
Return feedback items of feedback_type for owner_id, newest first.
| Parameter | Type | Description |
|---|---|---|
| `feedback_type` | FeedbackType | Type to filter by. |
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| `limit` | int | Maximum number of results (default: 100). |
| Type | Description |
|---|---|
| list[FeedbackItem] | Matching feedback items ordered by creation time descending. |
Compute summary statistics for owner_id in the last window_hours hours.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are aggregated. |
| `window_hours` | int | Look-back window in hours (default: 24). |
| Type | Description |
|---|---|
| FeedbackSummary | Aggregated FeedbackSummary for the window. |
FeedbackCollector
Section titled “FeedbackCollector”Collects and stores user feedback.
Provides methods to capture different types of feedback and integrate with ML retraining pipelines.
Example
collector = FeedbackCollector()# Collect ratingawait collector.collect_rating(rating=5,context={"model": "gpt-4", "input": "Hello"})# Collect correctionawait collector.collect_correction(original="incorrect output",corrected="correct output",context={"model_id": "123"})# Get all feedbackitems = collector.get_feedback()Initialize feedback collector.
| Parameter | Type | Description |
|---|---|---|
| `storage` | FeedbackStoreProtocol | None | Optional storage backend (e.g., database, file) If None, uses in-memory storage |
Collect a rating feedback.
| Parameter | Type | Description |
|---|---|---|
| `rating` | float | Numeric rating value. |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `context` | dict[str, Any] | None | Context about what was rated. |
| `metadata` | dict[str, Any] | None | Additional metadata. |
| Type | Description |
|---|---|
| str | Feedback ID. |
Collect text feedback.
| Parameter | Type | Description |
|---|---|---|
| `text` | str | Feedback text. |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `context` | dict[str, Any] | None | Context about what feedback is for. |
| `metadata` | dict[str, Any] | None | Additional metadata. |
| Type | Description |
|---|---|
| str | Feedback ID. |
Collect a correction feedback.
| Parameter | Type | Description |
|---|---|---|
| `original` | str | Original (incorrect) output. |
| `corrected` | str | Corrected output. |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `context` | dict[str, Any] | None | Context. |
| `metadata` | dict[str, Any] | None | Additional metadata. |
| Type | Description |
|---|---|
| str | Feedback ID. |
Collect ground truth label.
Useful for supervised learning and model retraining.
| Parameter | Type | Description |
|---|---|---|
| `label` | Any | Ground truth label. |
| `input_data` | Any | Input that this label corresponds to. |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `context` | dict[str, Any] | None | Context. |
| `metadata` | dict[str, Any] | None | Additional metadata. |
| Type | Description |
|---|---|
| str | Feedback ID. |
Get feedback items for an owner.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| `feedback_type` | FeedbackType | None | Optional filter by type. |
| `limit` | int | None | Maximum number of items to return. |
| Type | Description |
|---|---|
| list[FeedbackItem] | List of feedback items from memory or the storage backend. |
Get feedback as dictionaries for an owner.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are returned. |
| `feedback_type` | FeedbackType | None | Optional filter by type. |
| `limit` | int | None | Maximum number of items. |
| Type | Description |
|---|---|
| list[dict[str, Any]] | List of feedback dictionaries. |
Clear all feedback from the in-memory buffer.
FeedbackConfig
Section titled “FeedbackConfig”Configuration for AI feedback collection.
Loaded from the ai_feedback: key in application.yaml, with environment
variable overrides via ORI_AI_FEEDBACK__* prefix.
Check config is safe for the target environment.
FeedbackContext
Section titled “FeedbackContext”Context manager for feedback collection during operations.
Automatically captures context and results for feedback.
Example
collector = FeedbackCollector()async with FeedbackContext(collector, operation="prediction") as ctx:result = await model.predict(input_data)ctx.set_result(result)# Feedback context is automatically storedInitialize feedback context.
| Parameter | Type | Description |
|---|---|---|
| `collector` | FeedbackCollector | Feedback collector |
| `operation` | str | Operation name |
| `metadata` | dict[str, Any] | None | Additional metadata |
Set input data.
| Parameter | Type | Description |
|---|---|---|
| `input_data` | Any | Input to the operation |
Set operation result.
| Parameter | Type | Description |
|---|---|---|
| `result` | Any | Result of the operation |
FeedbackItem
Section titled “FeedbackItem”A single feedback item.
Attributes: feedback_type: Type of feedback (rating, text, correction, or label). value: The feedback value (e.g. rating score, text comment). owner_id: Owner scope for the item (user, tenant, or composite). context: Context about what was being evaluated (session_id, model, etc.). metadata: Additional metadata dictionary. id: Unique feedback identifier. created_at: Timestamp when feedback was created.
Alias for feedback_type for backward compatibility.
| Type | Description |
|---|---|
| FeedbackType | The feedback type. |
Convert to dictionary.
| Type | Description |
|---|---|
| dict[str, Any] | Dictionary representation with ISO-formatted timestamp. |
FeedbackMiddleware
Section titled “FeedbackMiddleware”Middleware for automatic feedback capture.
Captures prediction inputs/outputs automatically and provides hooks for user feedback collection.
When an authorize callback is supplied, feedback submissions
via create_feedback_endpoint() are gated on it. Leaving it unset
means the endpoint is open to anyone who can reach it — an explicit,
informed choice, not an enforced control.
Example
from oridecon.app import Applicationfrom oridecon.ai.feedback import FeedbackMiddleware, FeedbackCollector
app = Application()collector = FeedbackCollector()middleware = FeedbackMiddleware(collector)app.use(middleware)Initialize feedback middleware.
| Parameter | Type | Description |
|---|---|---|
| `collector` | FeedbackCollector | Feedback collector instance |
| `capture_inputs` | bool | Whether to capture request inputs |
| `capture_outputs` | bool | Whether to capture response outputs |
| `capture_metadata` | bool | Whether to capture additional metadata |
| `registry` | FeedbackProcessorRegistry | None | Feedback processor registry (defaults to one with built-in processors) |
| `authorize` | Callable[[FeedbackAuthContext], bool | Awaitable[bool]] | None | Optional callback gating feedback submissions. Receives the submission's context id and metadata; return True to accept, False to raise FeedbackAuthorizationError. Sync and async callables are supported. ``None`` (default) performs no check — the endpoint is open to anyone who can reach it. |
Create a feedback submission endpoint.
The returned handler first consults the middleware’s
authorize callback (if set); a denied submission raises
FeedbackAuthorizationError
before any processing.
| Type | Description |
|---|---|
| Callable | Async handler function for feedback submission |
Example
middleware = FeedbackMiddleware(collector)feedback_handler = middleware.create_feedback_endpoint()# Use with your web framework:# app.post("/feedback", feedback_handler)FeedbackModule
Section titled “FeedbackModule”AI Feedback collection and processing integration.
Call configure to register a FeedbackProtocol implementation along with the processor registry and storage backend for injection.
Usage
from oridecon.ai.feedback.config import FeedbackConfig
@module( imports=[ FeedbackModule.configure( FeedbackConfig(async_processing=False) ) ])class AppModule(Module): passfrom oridecon.ai.feedback.config import FeedbackConfig
@module( imports=[ FeedbackModule.configure( FeedbackConfig(async_processing=False) ) ])class AppModule(Module): passError Handling
The feedback services use the Result pattern for expected failures.Domain errors are available via the exported exception hierarchy::
from oridecon.ai.feedback.exceptions import ( FeedbackError, # base — catch-all FeedbackProcessingError, # processor pipeline failure FeedbackValidationError, # schema / data-validation failure )The feedback services use the Result pattern for expected failures.Domain errors are available via the exported exception hierarchyfrom oridecon.ai.feedback.exceptions import ( FeedbackError, # base — catch-all FeedbackProcessingError, # processor pipeline failure FeedbackValidationError, # schema / data-validation failure) from oridecon.ai.feedback.exceptions import ( FeedbackError, # base — catch-all FeedbackProcessingError, # processor pipeline failure FeedbackValidationError, # schema / data-validation failure )Exports: FeedbackProtocol, FeedbackError, FeedbackProcessingError, FeedbackValidationError
Create a FeedbackModule with the given configuration.
| Parameter | Type | Description |
|---|---|---|
| `config` | FeedbackConfig | None | FeedbackConfig, a plain ``dict`` of the same keys, or ``None`` for framework defaults. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
Create a FeedbackModule suitable for unit and integration testing.
Uses in-memory or no-op implementations with minimal side effects.
| Parameter | Type | Description |
|---|---|---|
| `config` | FeedbackConfig | None | Optional config override. Uses safe test defaults when None. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
FeedbackProcessedHook
Section titled “FeedbackProcessedHook”Payload fired when feedback processing finishes.
FeedbackProvider
Section titled “FeedbackProvider”Provider for AI Feedback.
Registers FeedbackCollector and FeedbackProcessorRegistry. Wires a durable storage backend (DB-backed, optionally cached) into the collector during boot.
Factory method for DI container setup.
Register the feedback services.
Wire a durable storage backend into FeedbackCollector.
Shutdown phase.
Health check — always healthy (in-process domain provider).
No external backend to ping.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Ignored for in-process providers. |
| Type | Description |
|---|---|
| HealthCheckResult | Always HEALTHY — no external backend to ping. |
FeedbackService
Section titled “FeedbackService”High-level feedback submission and querying service.
Implements FeedbackProtocol. Wraps a FeedbackStoreProtocol with trace-id correlation and aggregation logic.
| Parameter | Type | Description |
|---|---|---|
| `store` | Optional storage backend; when ``None`` the service operates in a degraded no-op mode and logs warnings instead of persisting. |
Submit feedback for a traced AI generation.
| Parameter | Type | Description |
|---|---|---|
| `trace_id` | str | Identifier of the AI generation trace. |
| `score` | float | Numeric feedback score (e.g. 0.0–1.0 or 1–5). |
| `owner_id` | str | Owner scope; the item is recorded under this owner. |
| `comment` | str | None | Optional free-text comment stored in metadata. |
| `metadata` | dict[str, Any] | None | Optional additional key-value metadata. |
| Exception | Description |
|---|---|
| FeedbackTooLargeError | If comment exceeds MAX_FEEDBACK_TEXT_LENGTH characters or the serialized metadata (including the folded comment) exceeds MAX_CONTEXT_SIZE characters. |
Get aggregate feedback statistics for an owner.
| Parameter | Type | Description |
|---|---|---|
| `owner_id` | str | Owner scope; only this owner's items are aggregated. |
| `model` | str | None | Optional model name for context (currently informational). |
| `provider` | str | None | Optional provider name for context (currently informational). |
| Type | Description |
|---|---|
| dict[str, Any] | Dictionary with ``total_count``, ``average_rating``, and ``by_type`` breakdown, plus optional ``model``/``provider`` keys when supplied. |
FeedbackStoredHook
Section titled “FeedbackStoredHook”Payload fired when feedback is persisted by a feedback store.
FeedbackSubmittedEvent
Section titled “FeedbackSubmittedEvent”Emitted when user feedback is submitted and persisted.
Consumed by: quality tracking, model improvement, audit.
FeedbackSubmittedHook
Section titled “FeedbackSubmittedHook”Payload fired when feedback is submitted to the feedback pipeline.
FeedbackSummary
Section titled “FeedbackSummary”Aggregated statistics for collected feedback items.
Attributes: total_count: Total number of feedback items in the window. average_rating: Mean rating across all RATING-type items, or None if no ratings were collected. count_by_type: Item count keyed by FeedbackType enum value (e.g. “rating”, “text”, “correction”, “label”).
FeedbackType
Section titled “FeedbackType”Type of feedback collected.
Exceptions
Section titled “Exceptions”FeedbackAuthorizationError
Section titled “FeedbackAuthorizationError”Raised when the endpoint's authorization callback denies a submission.
FeedbackError
Section titled “FeedbackError”Base exception for all feedback-related errors.
FeedbackProcessingError
Section titled “FeedbackProcessingError”Raised when a feedback processor fails.
FeedbackTooLargeError
Section titled “FeedbackTooLargeError”Raised when a payload exceeds MAX_FEEDBACK_TEXT_LENGTH or MAX_CONTEXT_SIZE.
FeedbackValidationError
Section titled “FeedbackValidationError”Raised when feedback data fails validation.