-
Notifications
You must be signed in to change notification settings - Fork 0
feat(llm): structured output (proposal 0016) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
861af60
chore: bump spec to v0.15.0; add jsonschema; skip deferred fixtures
chris-colinsky e9298af
feat(llm): add StructuredOutputInvalid error category
chris-colinsky 069cfc8
feat(llm): add Response.parsed field
chris-colinsky 5a889c7
feat(llm): validate_response_schema + strict_mode_supported helpers
chris-colinsky 8203605
feat(llm): Provider Protocol gains response_schema parameter
chris-colinsky 467f74b
feat(llm/openai): native response_format wire path + Pydantic overload
chris-colinsky 16fb3c4
feat(llm/openai): prompt-augmentation fallback + inspect property
chris-colinsky 66aa908
test(conformance): capability-agnostic harness helpers for wire + car…
chris-colinsky adf617c
test: drive 0016 fixtures 021-028 + add structured-output unit tests
chris-colinsky 1d1e2df
docs: changelog entry for proposal 0016 under [Unreleased]
chris-colinsky 03bcf23
fix(llm): address CoPilot review on PR #42
chris-colinsky 2d39a56
docs: upgrade hello-world to demo structured output
chris-colinsky e7cb0de
fix(examples): hello-world base_url default and observer trace
chris-colinsky c9326e8
docs(examples): wire research + summarize nodes to real LLM calls
chris-colinsky 1ae405b
docs: structured output concepts page + model-providers updates
chris-colinsky 58f6c2f
docs: fix Provider.complete docstring Returns rendering
chris-colinsky 8ed334c
fix(llm): second CoPilot review pass on PR #42
chris-colinsky 1b5fbb0
fix: third CoPilot review pass on PR #42
chris-colinsky cddb2b1
fix: fourth CoPilot review pass on PR #42
chris-colinsky b283f97
docs: cumulative strict-mode constraint list + spec-version-drift test
chris-colinsky 4c12add
fix: fifth CoPilot review pass on PR #42
chris-colinsky b8ffc43
fix(test): replace git-describe submodule check with CHANGELOG parse
chris-colinsky 578322d
fix(test): satisfy CodeQL on the changelog-parsing test
chris-colinsky e5f1426
fix: seventh CoPilot review pass on PR #42
chris-colinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| """Hello-world demo: a 3-node graph where each node makes an LLM call | ||
| with structured output. Classify a query, then either plan research or | ||
| write a one-sentence summary. | ||
|
|
||
| **Demonstrates:** | ||
|
|
||
| - Typed ``State`` with three reducer policies (``last_write_wins``, | ||
| ``append``, ``merge``). | ||
| - ``OpenAIProvider`` from ``openarmature.llm`` against any | ||
| OpenAI-compatible endpoint. | ||
| - Both ``response_schema`` forms: | ||
| - Pydantic class (``Classification``, ``Summary``): typed | ||
| instance on ``Response.parsed``. | ||
| - JSON Schema dict (``research``): raw dict on ``Response.parsed``. | ||
| - Conditional routing on a parsed field (``route`` reads | ||
| ``state.classification.intent``). | ||
| - ``attach_observer`` for boundary visibility. | ||
|
|
||
| **Configuration** (env vars; OpenAI defaults shown): | ||
|
|
||
| - ``LLM_BASE_URL``: defaults to ``https://api.openai.com``. **Host | ||
| root only**; the impl adds ``/v1/chat/completions`` and | ||
| ``/v1/models`` itself, so do NOT include ``/v1`` in this value. | ||
| - ``LLM_MODEL``: defaults to ``gpt-4o-mini``. | ||
| - ``LLM_API_KEY``: required (your OpenAI API key, or empty for | ||
| local servers that don't authenticate). | ||
|
|
||
| Run with: | ||
|
|
||
| uv sync --group examples | ||
| LLM_API_KEY=sk-... uv run python examples/00-hello-world/main.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import os | ||
| from collections.abc import Mapping | ||
| from typing import Annotated, Any, Literal | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| from openarmature.graph import ( | ||
| END, | ||
| CompiledGraph, | ||
| GraphBuilder, | ||
| NodeEvent, | ||
| State, | ||
| append, | ||
| merge, | ||
| ) | ||
| from openarmature.llm import OpenAIProvider, UserMessage | ||
|
|
||
|
|
||
| # Pydantic schemas the model is constrained to produce. Passing a | ||
| # class as ``response_schema`` makes the framework convert to JSON | ||
| # Schema, instruct the provider to return matching content, validate | ||
| # the response, and yield an instance via ``Response.parsed``. | ||
| class Classification(BaseModel): | ||
| intent: Literal["research", "summarize"] | ||
| rationale: str | ||
|
|
||
|
|
||
| class Summary(BaseModel): | ||
| one_liner: str | ||
| confidence: float | ||
|
|
||
|
|
||
| # State holds intermediate artifacts from each LLM call. ``research`` | ||
| # uses a dict schema (rather than a class), so its parsed value is a | ||
| # raw dict, typed here as ``dict[str, Any] | None``. | ||
| class PipelineState(State): | ||
| query: str | ||
| classification: Classification | None = None | ||
| research_plan: dict[str, Any] | None = None | ||
| summary: Summary | None = None | ||
| sources: Annotated[list[str], append] = Field(default_factory=list) | ||
| metadata: Annotated[dict[str, str], merge] = Field(default_factory=dict) | ||
|
|
||
|
|
||
| _provider = OpenAIProvider( | ||
| base_url=os.environ.get("LLM_BASE_URL", "https://api.openai.com"), | ||
| model=os.environ.get("LLM_MODEL", "gpt-4o-mini"), | ||
| api_key=os.environ.get("LLM_API_KEY"), | ||
|
chris-colinsky marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
|
|
||
| async def classify(state: PipelineState) -> Mapping[str, Any]: | ||
| # response_schema=class form: parsed comes back as a Classification | ||
| # instance. The model picks the branch (research vs summarize) and | ||
| # the routing function below reads it as a typed field. | ||
| response = await _provider.complete( | ||
| [ | ||
| UserMessage( | ||
| content=( | ||
| f"Route this query to either 'research' (find new information) " | ||
| f"or 'summarize' (condense known material): {state.query!r}" | ||
| ) | ||
| ) | ||
| ], | ||
| response_schema=Classification, | ||
| ) | ||
| return {"classification": response.parsed, "metadata": {"classified_by": "llm"}} | ||
|
|
||
|
|
||
| async def research(state: PipelineState) -> Mapping[str, Any]: | ||
| # response_schema=dict form: parsed comes back as a plain dict. | ||
| # Same wire shape as the class form: the framework converts a | ||
| # class via .model_json_schema() under the hood. Use dict when | ||
| # you want raw shape without declaring a Pydantic model. | ||
| response = await _provider.complete( | ||
| [ | ||
| UserMessage( | ||
| content=( | ||
| f"Plan research for the query {state.query!r}. List up to 3 " | ||
| f"specific topics to investigate and up to 3 follow-up questions." | ||
| ) | ||
| ) | ||
| ], | ||
| response_schema={ | ||
| "type": "object", | ||
| "properties": { | ||
| "topics": {"type": "array", "items": {"type": "string"}}, | ||
| "follow_up_questions": {"type": "array", "items": {"type": "string"}}, | ||
| }, | ||
| "required": ["topics", "follow_up_questions"], | ||
| "additionalProperties": False, | ||
| }, | ||
| ) | ||
| return { | ||
| "research_plan": response.parsed, | ||
| "sources": ["wikipedia", "arxiv"], | ||
| "metadata": {"tool": "research"}, | ||
| } | ||
|
|
||
|
|
||
| async def summarize(state: PipelineState) -> Mapping[str, Any]: | ||
| # Pydantic-class form again: parsed is a Summary instance with | ||
| # a typed one_liner and a confidence float. | ||
| response = await _provider.complete( | ||
| [ | ||
| UserMessage( | ||
| content=( | ||
| f"Summarize {state.query!r} in one sentence. Set confidence " | ||
| f"between 0 and 1 reflecting how well-established the answer is." | ||
| ) | ||
| ) | ||
| ], | ||
| response_schema=Summary, | ||
| ) | ||
| return { | ||
| "summary": response.parsed, | ||
| "sources": ["cache"], | ||
| "metadata": {"tool": "summarize"}, | ||
| } | ||
|
|
||
|
|
||
| def route(state: PipelineState) -> str: | ||
| if state.classification is None: | ||
| raise RuntimeError("classify did not populate state.classification") | ||
| return state.classification.intent | ||
|
|
||
|
|
||
| async def trace(event: NodeEvent) -> None: | ||
| # OpenAIProvider emits NodeEvent-shaped events for LLM-span | ||
| # tracking under a sentinel namespace; those have post_state=None. | ||
| # Filter to events that carry a state snapshot before reading it. | ||
| if event.phase == "completed" and event.error is None and event.post_state is not None: | ||
| print(f"{event.node_name}: sources={event.post_state.sources}") | ||
|
|
||
|
|
||
| def build_graph() -> CompiledGraph[PipelineState]: | ||
| return ( | ||
| GraphBuilder(PipelineState) | ||
| .add_node("classify", classify) | ||
| .add_node("research", research) | ||
| .add_node("summarize", summarize) | ||
| .add_conditional_edge("classify", route) | ||
| .add_edge("research", END) | ||
| .add_edge("summarize", END) | ||
| .set_entry("classify") | ||
| .compile() | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| graph = build_graph() | ||
| graph.attach_observer(trace) | ||
| try: | ||
| final = await graph.invoke(PipelineState(query="what is RAG?")) | ||
| print(f"\nclassification: {final.classification}") | ||
| if final.research_plan is not None: | ||
| print(f"research_plan: {final.research_plan}") | ||
| if final.summary is not None: | ||
| print(f"summary: {final.summary}") | ||
| print(f"sources: {final.sources}") | ||
| print(f"metadata: {final.metadata}") | ||
| finally: | ||
| await graph.drain() | ||
|
chris-colinsky marked this conversation as resolved.
chris-colinsky marked this conversation as resolved.
chris-colinsky marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.