jxnl / jxnl/structuredoutputsbyexample

Feedback/Suggestions

Open
#7 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
13
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Awesome repo/site!

Just my thoughts on getting better results on harder tasks which might be interesting to add:

1. Use enums as little as possible vs Literal but mapping back into Enum when needed - can significantly improve results due to tokens being closer (Literal is next to the object vs Enum is a reference to another object in the schema - helping with attention)
2. Chain of Thought/Reasoning on complex fields (example 2)
3. Description in fields can be super powerful. Plus the tokens are close to the structured output model which helps with attention vs user/system message
4. Referencing context directly before output (trigger_type_references)

Example Model 1:

```python
class BaseBarrierTermsExtractionModel(BaseModel):
trigger_type_references: list[str] = Field(..., description="Text used to determine the trigger type.")
trigger_type_reason: str = Field(..., description="Explicit reasoning why you chose the specific trigger type.")
trigger_type: Literal["GREATER", "LESS", "EQUAL", "GREATER_THAN_OR_EQUAL", "LESS_THAN_OR_EQUAL"] = Field(
...,
description="Comparison operator for the barrier level to be breached (Down is LESS, Up is GREATER). Use the triggerTypeReferences to determine the trigger type.",
)

@field_validator("trigger_type", mode="after")
def transform_trigger_type(cls, v):
mapping = {
"LESS_THAN_OR_EQUAL": TriggerType.EQUAL_OR_LESS,
"GREATER_THAN_OR_EQUAL": TriggerType.EQUAL_OR_GREATER,
"EQUAL": TriggerType.EQUAL,
"LESS": TriggerType.LESS,
"GREATER": TriggerType.GREATER,
}
return mapping.get(v, v)
```

Data Extraction when value isn't explicit or requires a forumla.

```python
class DateExtractionModel(DataModel):
"""Used when the date isn't explicitly mentioned in the document."""

trading_date_reference: str = Field(
..., description="Text reference for the trading date (e.g., 'Two currency business days following the Expiration Date')."
)
trading_date_reference_with_variables: str = Field(
..., description=("Text reference with variables filled in (e.g., 'Expiration Date -> Two currency business days following the 2025-01-01').")
)
start_date: dt.date = Field(..., description="The start date for the calculation.")
relative: str = Field(
..., description="The relative date to add to the start date. i.e. 2B for 2 business days. Must be in the form of xB where x is the number of days"
)

@model_validator(mode="after")
def resolve_date():
# date math here and set a field to use later
pass
````

Contributor guide

Open the contributing guide

Research direction

No file, test, or entry point is named; first identify which examples or implementation area should own these suggestions. Review the existing structured-output examples and maintainer feedback to narrow the proposal into a defined change with clear acceptance criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.