kedi-typesafe 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. kedi_typesafe-0.1.0/.gitignore +218 -0
  2. kedi_typesafe-0.1.0/.python-version +1 -0
  3. kedi_typesafe-0.1.0/LICENSE +21 -0
  4. kedi_typesafe-0.1.0/Makefile +32 -0
  5. kedi_typesafe-0.1.0/PKG-INFO +181 -0
  6. kedi_typesafe-0.1.0/README.md +156 -0
  7. kedi_typesafe-0.1.0/examples/langchain_agent.py +22 -0
  8. kedi_typesafe-0.1.0/examples/langchain_contacts.py +29 -0
  9. kedi_typesafe-0.1.0/examples/pydantic_agent.py +27 -0
  10. kedi_typesafe-0.1.0/examples/pydantic_contacts.py +25 -0
  11. kedi_typesafe-0.1.0/pyproject.toml +76 -0
  12. kedi_typesafe-0.1.0/pyrightconfig.json +11 -0
  13. kedi_typesafe-0.1.0/src/kedi_typesafe/__init__.py +29 -0
  14. kedi_typesafe-0.1.0/src/kedi_typesafe/core/__init__.py +19 -0
  15. kedi_typesafe-0.1.0/src/kedi_typesafe/core/errors.py +25 -0
  16. kedi_typesafe-0.1.0/src/kedi_typesafe/core/evaluation.py +282 -0
  17. kedi_typesafe-0.1.0/src/kedi_typesafe/core/extraction.py +115 -0
  18. kedi_typesafe-0.1.0/src/kedi_typesafe/core/schema.py +307 -0
  19. kedi_typesafe-0.1.0/src/kedi_typesafe/integrations/__init__.py +5 -0
  20. kedi_typesafe-0.1.0/src/kedi_typesafe/integrations/langchain.py +334 -0
  21. kedi_typesafe-0.1.0/src/kedi_typesafe/integrations/pydantic.py +190 -0
  22. kedi_typesafe-0.1.0/tests/core/test_evaluation.py +449 -0
  23. kedi_typesafe-0.1.0/tests/core/test_extraction.py +96 -0
  24. kedi_typesafe-0.1.0/tests/core/test_schema.py +384 -0
  25. kedi_typesafe-0.1.0/tests/integrations/test_langchain.py +339 -0
  26. kedi_typesafe-0.1.0/tests/integrations/test_live.py +32 -0
  27. kedi_typesafe-0.1.0/tests/integrations/test_pydantic.py +382 -0
  28. kedi_typesafe-0.1.0/uv.lock +1999 -0
@@ -0,0 +1,218 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+ # Temporary file for partial code execution
204
+ tempCodeRunnerFile.py
205
+
206
+ # Ruff stuff:
207
+ .ruff_cache/
208
+
209
+ # PyPI configuration file
210
+ .pypirc
211
+
212
+ # Marimo
213
+ marimo/_static/
214
+ marimo/_lsp/
215
+ __marimo__/
216
+
217
+ # Streamlit
218
+ .streamlit/secrets.toml
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kedi Programming Language
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,32 @@
1
+ UV ?= uv
2
+
3
+ .PHONY: format format-check lint ty basedpyright check tests coverage build prod
4
+
5
+ format:
6
+ $(UV) run --group dev ruff check --fix .
7
+ $(UV) run --group dev ruff format .
8
+
9
+ format-check:
10
+ $(UV) run --group dev ruff format --check .
11
+
12
+ lint:
13
+ $(UV) run --group dev ruff check .
14
+
15
+ ty:
16
+ $(UV) run --group dev ty check src
17
+
18
+ basedpyright:
19
+ $(UV) run --group dev basedpyright
20
+
21
+ check: lint format-check ty basedpyright
22
+
23
+ tests:
24
+ $(UV) run --group dev pytest -m "not live"
25
+
26
+ coverage:
27
+ $(UV) run --group dev pytest -m "not live" --cov --cov-branch --cov-fail-under=100
28
+
29
+ build:
30
+ $(UV) build
31
+
32
+ prod: check coverage build
@@ -0,0 +1,181 @@
1
+ Metadata-Version: 2.5
2
+ Name: kedi-typesafe
3
+ Version: 0.1.0
4
+ Summary: TypeSafe AI integrations for Kedi
5
+ Project-URL: Repository, https://github.com/kedi-lang/kedi-typesafe
6
+ Project-URL: Issues, https://github.com/kedi-lang/kedi-typesafe/issues
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Requires-Python: >=3.10
10
+ Requires-Dist: pydantic-ai-slim==2.36.0
11
+ Requires-Dist: typesafe-sdk==0.6.0
12
+ Provides-Extra: all
13
+ Requires-Dist: email-validator<3,>=2.3; extra == 'all'
14
+ Requires-Dist: langchain==1.3.18; extra == 'all'
15
+ Requires-Dist: phonenumbers<10,>=9; extra == 'all'
16
+ Requires-Dist: pydantic-extra-types<3,>=2.10; extra == 'all'
17
+ Provides-Extra: email
18
+ Requires-Dist: email-validator<3,>=2.3; extra == 'email'
19
+ Provides-Extra: langchain
20
+ Requires-Dist: langchain==1.3.18; extra == 'langchain'
21
+ Provides-Extra: phone
22
+ Requires-Dist: phonenumbers<10,>=9; extra == 'phone'
23
+ Requires-Dist: pydantic-extra-types<3,>=2.10; extra == 'phone'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # kedi-typesafe
27
+
28
+ Framework-native integrations for using TypeSafe AI's Jev models with Kedi's supported agent
29
+ frameworks. Core Jev schema translation and the Pydantic AI and LangChain integrations ship as
30
+ one `kedi-typesafe` package.
31
+
32
+ ## Pydantic AI
33
+
34
+ Install the package and provide `TYPESAFE_API_KEY` in your environment:
35
+
36
+ ```bash
37
+ pip install kedi-typesafe
38
+ export TYPESAFE_API_KEY="your-key"
39
+ ```
40
+
41
+ ```python
42
+ from typing import Literal
43
+
44
+ from pydantic import BaseModel, Field
45
+ from pydantic_ai import Agent
46
+
47
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
48
+
49
+
50
+ class Triage(BaseModel):
51
+ urgent: bool = Field(description="The request needs action today")
52
+ route: Literal["billing", "support", "other"] = Field(
53
+ description="Which team should handle the request?"
54
+ )
55
+
56
+
57
+ model = TypeSafeModel("jev-latest")
58
+ agent = Agent(model, output_type=Triage)
59
+ result = agent.run_sync("I was charged twice. Please fix this today.")
60
+ print(result.output)
61
+ ```
62
+
63
+ The initial surface supports booleans, finite string `Literal`/`Enum` values, constrained text
64
+ extraction, and flat Pydantic models composed of those fields. Pydantic's native `EmailStr` and
65
+ `pydantic-extra-types` `PhoneNumber` schemas are recognized automatically. A JSON Schema `pattern`
66
+ is treated as a regex candidate extractor; other string fields can provide an explicit field
67
+ extractor:
68
+
69
+ Install `kedi-typesafe[email]`, `kedi-typesafe[phone]`, or `kedi-typesafe[all]` when those optional
70
+ Pydantic field types are used.
71
+
72
+ ```python
73
+ from kedi_typesafe import RegexExtractor
74
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
75
+
76
+ model = TypeSafeModel(
77
+ text_extractors={"ticket_id": RegexExtractor(pattern=r"CASE-\d+")},
78
+ )
79
+ ```
80
+
81
+ Candidate extraction is deterministic and Jev only selects among values found in state, following
82
+ TypeSafe's pre-parsed extraction pattern. Jev is not a free-form text-generation model, so
83
+ unconstrained strings, tool calling, streaming, arbitrary numbers, nested models, and optional
84
+ fields are rejected before a provider request is made.
85
+
86
+ Boolean answers use Jev's Noul probability and a configurable threshold. The default decision is
87
+ strictly `probability > 0.5`; the full probability remains available in model response metadata.
88
+
89
+ ## LangChain
90
+
91
+ Install `kedi-typesafe[langchain]` and use the native structured-output surface:
92
+
93
+ ```python
94
+ from typing import Literal
95
+
96
+ from langchain.agents import create_agent
97
+ from pydantic import BaseModel, Field
98
+
99
+ from kedi_typesafe.integrations.langchain import TypeSafeChatModel
100
+
101
+
102
+ class Triage(BaseModel):
103
+ urgent: bool = Field(description="The request needs action today")
104
+ route: Literal["billing", "support", "other"] = Field(
105
+ description="Which team should handle the request?"
106
+ )
107
+
108
+
109
+ agent = create_agent(TypeSafeChatModel("jev-latest"), response_format=Triage)
110
+ result = agent.invoke(
111
+ {"messages": [{"role": "user", "content": "I was charged twice. Fix this today."}]}
112
+ )
113
+ print(result["structured_response"])
114
+ ```
115
+
116
+ `TypeSafeChatModel.with_structured_output(...)` is also supported for direct LangChain model use.
117
+ The same Jev schema restrictions described above apply. Free-form invocation, function tools,
118
+ media/tool history, stop sequences, and arbitrary model settings fail before a provider request.
119
+
120
+ ## Email and Phone Extraction
121
+
122
+ Install the field validators:
123
+
124
+ ```bash
125
+ pip install 'kedi-typesafe[email,phone]'
126
+ ```
127
+
128
+ ```python
129
+ from pydantic import BaseModel, EmailStr, Field
130
+ from pydantic_ai import Agent
131
+ from pydantic_extra_types.phone_numbers import PhoneNumber
132
+
133
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
134
+
135
+
136
+ class Contact(BaseModel):
137
+ email: EmailStr = Field(description="The current support email, not the archived address")
138
+ phone: PhoneNumber = Field(description="The current support phone number")
139
+
140
+
141
+ agent = Agent(TypeSafeModel(), output_type=Contact)
142
+ result = agent.run_sync(
143
+ "Archived contact: old@example.com. Current support: help@example.com, +1 202-555-0100."
144
+ )
145
+ print(result.output)
146
+ ```
147
+
148
+ Email and phone candidates are extracted from the supplied text automatically. Jev selects the
149
+ candidate matching the field description; Pydantic then validates the result. Phone numbers use
150
+ Pydantic's normalized format, for example `tel:+1-202-555-0100`. Provide international numbers
151
+ with a country code. Missing candidates or a no-match answer raise an extraction error rather
152
+ than inventing a value. All fields in this initial integration must be required.
153
+
154
+ For LangChain, use the same `Contact` schema with
155
+ `create_agent(TypeSafeChatModel(), response_format=Contact)` and install
156
+ `kedi-typesafe[langchain,email,phone]`.
157
+
158
+ ## Runnable Examples
159
+
160
+ After cloning this repository, run `uv sync --all-extras` and set `TYPESAFE_API_KEY`.
161
+
162
+ | Example | Command |
163
+ | --- | --- |
164
+ | Pydantic classification | `uv run examples/pydantic_agent.py` |
165
+ | LangChain classification | `uv run examples/langchain_agent.py` |
166
+ | Pydantic email and phone | `uv run examples/pydantic_contacts.py` |
167
+ | LangChain email and phone | `uv run examples/langchain_contacts.py` |
168
+
169
+ These examples call the real TypeSafe API. An async context manager closes owned connections
170
+ after use; it is optional for constructing the model. Reusing a model within the same event
171
+ loop allows its HTTP connection to be reused.
172
+
173
+ ## Development
174
+
175
+ ```bash
176
+ uv sync --all-extras --group dev
177
+ make prod
178
+ ```
179
+
180
+ Normal tests use deterministic typed fakes and never call TypeSafe. Live API validation is kept
181
+ separate so CI and local quality gates do not consume credentials or incur cost.
@@ -0,0 +1,156 @@
1
+ # kedi-typesafe
2
+
3
+ Framework-native integrations for using TypeSafe AI's Jev models with Kedi's supported agent
4
+ frameworks. Core Jev schema translation and the Pydantic AI and LangChain integrations ship as
5
+ one `kedi-typesafe` package.
6
+
7
+ ## Pydantic AI
8
+
9
+ Install the package and provide `TYPESAFE_API_KEY` in your environment:
10
+
11
+ ```bash
12
+ pip install kedi-typesafe
13
+ export TYPESAFE_API_KEY="your-key"
14
+ ```
15
+
16
+ ```python
17
+ from typing import Literal
18
+
19
+ from pydantic import BaseModel, Field
20
+ from pydantic_ai import Agent
21
+
22
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
23
+
24
+
25
+ class Triage(BaseModel):
26
+ urgent: bool = Field(description="The request needs action today")
27
+ route: Literal["billing", "support", "other"] = Field(
28
+ description="Which team should handle the request?"
29
+ )
30
+
31
+
32
+ model = TypeSafeModel("jev-latest")
33
+ agent = Agent(model, output_type=Triage)
34
+ result = agent.run_sync("I was charged twice. Please fix this today.")
35
+ print(result.output)
36
+ ```
37
+
38
+ The initial surface supports booleans, finite string `Literal`/`Enum` values, constrained text
39
+ extraction, and flat Pydantic models composed of those fields. Pydantic's native `EmailStr` and
40
+ `pydantic-extra-types` `PhoneNumber` schemas are recognized automatically. A JSON Schema `pattern`
41
+ is treated as a regex candidate extractor; other string fields can provide an explicit field
42
+ extractor:
43
+
44
+ Install `kedi-typesafe[email]`, `kedi-typesafe[phone]`, or `kedi-typesafe[all]` when those optional
45
+ Pydantic field types are used.
46
+
47
+ ```python
48
+ from kedi_typesafe import RegexExtractor
49
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
50
+
51
+ model = TypeSafeModel(
52
+ text_extractors={"ticket_id": RegexExtractor(pattern=r"CASE-\d+")},
53
+ )
54
+ ```
55
+
56
+ Candidate extraction is deterministic and Jev only selects among values found in state, following
57
+ TypeSafe's pre-parsed extraction pattern. Jev is not a free-form text-generation model, so
58
+ unconstrained strings, tool calling, streaming, arbitrary numbers, nested models, and optional
59
+ fields are rejected before a provider request is made.
60
+
61
+ Boolean answers use Jev's Noul probability and a configurable threshold. The default decision is
62
+ strictly `probability > 0.5`; the full probability remains available in model response metadata.
63
+
64
+ ## LangChain
65
+
66
+ Install `kedi-typesafe[langchain]` and use the native structured-output surface:
67
+
68
+ ```python
69
+ from typing import Literal
70
+
71
+ from langchain.agents import create_agent
72
+ from pydantic import BaseModel, Field
73
+
74
+ from kedi_typesafe.integrations.langchain import TypeSafeChatModel
75
+
76
+
77
+ class Triage(BaseModel):
78
+ urgent: bool = Field(description="The request needs action today")
79
+ route: Literal["billing", "support", "other"] = Field(
80
+ description="Which team should handle the request?"
81
+ )
82
+
83
+
84
+ agent = create_agent(TypeSafeChatModel("jev-latest"), response_format=Triage)
85
+ result = agent.invoke(
86
+ {"messages": [{"role": "user", "content": "I was charged twice. Fix this today."}]}
87
+ )
88
+ print(result["structured_response"])
89
+ ```
90
+
91
+ `TypeSafeChatModel.with_structured_output(...)` is also supported for direct LangChain model use.
92
+ The same Jev schema restrictions described above apply. Free-form invocation, function tools,
93
+ media/tool history, stop sequences, and arbitrary model settings fail before a provider request.
94
+
95
+ ## Email and Phone Extraction
96
+
97
+ Install the field validators:
98
+
99
+ ```bash
100
+ pip install 'kedi-typesafe[email,phone]'
101
+ ```
102
+
103
+ ```python
104
+ from pydantic import BaseModel, EmailStr, Field
105
+ from pydantic_ai import Agent
106
+ from pydantic_extra_types.phone_numbers import PhoneNumber
107
+
108
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
109
+
110
+
111
+ class Contact(BaseModel):
112
+ email: EmailStr = Field(description="The current support email, not the archived address")
113
+ phone: PhoneNumber = Field(description="The current support phone number")
114
+
115
+
116
+ agent = Agent(TypeSafeModel(), output_type=Contact)
117
+ result = agent.run_sync(
118
+ "Archived contact: old@example.com. Current support: help@example.com, +1 202-555-0100."
119
+ )
120
+ print(result.output)
121
+ ```
122
+
123
+ Email and phone candidates are extracted from the supplied text automatically. Jev selects the
124
+ candidate matching the field description; Pydantic then validates the result. Phone numbers use
125
+ Pydantic's normalized format, for example `tel:+1-202-555-0100`. Provide international numbers
126
+ with a country code. Missing candidates or a no-match answer raise an extraction error rather
127
+ than inventing a value. All fields in this initial integration must be required.
128
+
129
+ For LangChain, use the same `Contact` schema with
130
+ `create_agent(TypeSafeChatModel(), response_format=Contact)` and install
131
+ `kedi-typesafe[langchain,email,phone]`.
132
+
133
+ ## Runnable Examples
134
+
135
+ After cloning this repository, run `uv sync --all-extras` and set `TYPESAFE_API_KEY`.
136
+
137
+ | Example | Command |
138
+ | --- | --- |
139
+ | Pydantic classification | `uv run examples/pydantic_agent.py` |
140
+ | LangChain classification | `uv run examples/langchain_agent.py` |
141
+ | Pydantic email and phone | `uv run examples/pydantic_contacts.py` |
142
+ | LangChain email and phone | `uv run examples/langchain_contacts.py` |
143
+
144
+ These examples call the real TypeSafe API. An async context manager closes owned connections
145
+ after use; it is optional for constructing the model. Reusing a model within the same event
146
+ loop allows its HTTP connection to be reused.
147
+
148
+ ## Development
149
+
150
+ ```bash
151
+ uv sync --all-extras --group dev
152
+ make prod
153
+ ```
154
+
155
+ Normal tests use deterministic typed fakes and never call TypeSafe. Live API validation is kept
156
+ separate so CI and local quality gates do not consume credentials or incur cost.
@@ -0,0 +1,22 @@
1
+ from typing import Literal
2
+
3
+ from langchain.agents import create_agent
4
+ from pydantic import BaseModel, Field
5
+
6
+ from kedi_typesafe.integrations.langchain import TypeSafeChatModel
7
+
8
+
9
+ class Triage(BaseModel):
10
+ urgent: bool = Field(description="The request needs action today")
11
+ route: Literal["billing", "support", "other"] = Field(
12
+ description="Which team should handle the request?"
13
+ )
14
+
15
+
16
+ with TypeSafeChatModel("jev-latest") as model:
17
+ agent = create_agent(model, response_format=Triage)
18
+ result = agent.invoke(
19
+ {"messages": [{"role": "user", "content": "I was charged twice. Fix this today."}]}
20
+ )
21
+
22
+ print(result["structured_response"])
@@ -0,0 +1,29 @@
1
+ from langchain.agents import create_agent
2
+ from pydantic import BaseModel, EmailStr, Field
3
+ from pydantic_extra_types.phone_numbers import PhoneNumber
4
+
5
+ from kedi_typesafe.integrations.langchain import TypeSafeChatModel
6
+
7
+
8
+ class Contact(BaseModel):
9
+ email: EmailStr = Field(description="The current support email, not the archived address")
10
+ phone: PhoneNumber = Field(description="The current support phone number")
11
+
12
+
13
+ with TypeSafeChatModel() as model:
14
+ agent = create_agent(model, response_format=Contact)
15
+ result = agent.invoke(
16
+ {
17
+ "messages": [
18
+ {
19
+ "role": "user",
20
+ "content": (
21
+ "Archived contact: old@example.com. "
22
+ "Current support: help@example.com, +1 202-555-0100."
23
+ ),
24
+ }
25
+ ]
26
+ }
27
+ )
28
+ contact = Contact.model_validate(result["structured_response"])
29
+ print(contact.model_dump_json(indent=2))
@@ -0,0 +1,27 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ from typing import Literal
5
+
6
+ from pydantic import BaseModel, Field
7
+ from pydantic_ai import Agent
8
+
9
+ from kedi_typesafe.integrations.pydantic import TypeSafeModel
10
+
11
+
12
+ class Triage(BaseModel):
13
+ urgent: bool = Field(description="The request needs action today")
14
+ route: Literal["billing", "support", "other"] = Field(
15
+ description="Which team should handle the request?"
16
+ )
17
+
18
+
19
+ async def main() -> None:
20
+ async with TypeSafeModel("jev-latest") as model:
21
+ agent = Agent(model, output_type=Triage)
22
+ result = await agent.run("I was charged twice. Please fix this today.")
23
+ print(result.output)
24
+
25
+
26
+ if __name__ == "__main__":
27
+ asyncio.run(main())