Protocol & Types
protocol
codex_ai.core.protocol
Core types and contracts for the LLM abstraction layer.
PromptResult — frozen DTO returned by every prompt builder. LLMProviderProtocol — adapter contract for LLM backends (OpenAI, Gemini, etc.). TextGenerationProvider — optional adapter contract for direct text generation. JsonGenerationProvider — optional adapter contract for direct JSON generation. ImageGenerationProvider — optional adapter contract for binary image generation. ImagenGenerationProvider — optional adapter contract for Imagen image generation. PromptBuilder — type alias for async builder functions registered via LLMRouter.
Classes
LLMMessage
Bases: BaseDTO
Strict contract for single message exchanged with LLM.
Source code in src/codex_ai/core/protocol.py
29 30 31 32 33 34 35 | |
PromptResult
Bases: BaseDTO
Frozen DTO produced by a prompt builder.
Passed directly to the LLM provider's answer() method.
Attributes:
| Name | Type | Description |
|---|---|---|
messages |
list[LLMMessage]
|
Provider-specific message list (OpenAI ChatCompletionMessageParam format or Gemini contents). Each provider interprets this field as needed. |
system |
str
|
Optional system/developer instruction (top-level string, used by Gemini and OpenAI o-series models that accept a dedicated system field). |
Example
result = PromptResult(
messages=[LLMMessage(role="user", content="Hello!")],
system="You are a helpful assistant.",
)
Source code in src/codex_ai/core/protocol.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
LLMProviderProtocol
Bases: Protocol
Adapter contract for LLM backends.
Implement this protocol to add a new provider (OpenAI, Gemini, Anthropic, etc.).
Example
class MockProvider:
async def answer(self, prompt: PromptResult, **kw: Any) -> str:
return "mocked response"
Source code in src/codex_ai/core/protocol.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
Functions
answer(prompt, **kw)
async
Send prompt to the LLM and return the response text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
PromptResult
|
Frozen DTO with messages and system instruction. |
required |
**kw
|
Any
|
Extra provider-specific kwargs (temperature, max_tokens, etc.). |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Response text from the LLM. |
Source code in src/codex_ai/core/protocol.py
83 84 85 86 87 88 89 90 91 92 93 94 | |
TextGenerationProvider
Bases: Protocol
Optional adapter contract for direct provider text generation.
Source code in src/codex_ai/core/protocol.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
Functions
generate_text(prompt, *, model=None, **kwargs)
async
Generate plain text from a prompt DTO or a raw prompt string.
Source code in src/codex_ai/core/protocol.py
103 104 105 106 107 108 109 110 111 112 113 | |
JsonGenerationProvider
Bases: Protocol
Optional adapter contract for provider-native JSON generation.
Source code in src/codex_ai/core/protocol.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
Functions
generate_json(prompt, *, schema=None, model=None, **kwargs)
async
Generate JSON and validate it locally when a Pydantic schema is provided.
Source code in src/codex_ai/core/protocol.py
122 123 124 125 126 127 128 129 130 131 132 133 | |
ImageGenerationProvider
Bases: Protocol
Optional adapter contract for providers that can generate image bytes.
This is intentionally separate from LLMProviderProtocol.answer() because
image generation returns binary parts, not text.
Source code in src/codex_ai/core/protocol.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Functions
generate_image_bytes(prompt, *, model=None, response_mime_type='image/webp', image_config=None, **kwargs)
async
Generate an image and return raw bytes plus the actual content type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Plain image-generation prompt. |
required |
model
|
str | None
|
Optional image model override. |
None
|
response_mime_type
|
str
|
Requested/preferred image MIME type. |
'image/webp'
|
image_config
|
dict[str, Any] | None
|
Optional image generation controls such as
|
None
|
**kwargs
|
Any
|
Extra provider-specific kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[bytes, str]
|
Tuple of |
Source code in src/codex_ai/core/protocol.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
ImagenGenerationProvider
Bases: Protocol
Optional adapter contract for providers that expose Imagen image generation.
This is separate from ImageGenerationProvider because Gemini image models
and Imagen models use different SDK methods and MIME configuration fields.
Source code in src/codex_ai/core/protocol.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
Functions
generate_imagen_bytes(prompt, *, model=None, response_mime_type='image/jpeg', **kwargs)
async
Generate an Imagen image and return raw bytes plus the actual content type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Plain image-generation prompt. |
required |
model
|
str | None
|
Optional Imagen model override. |
None
|
response_mime_type
|
str
|
Requested image MIME type passed to Imagen when supported. |
'image/jpeg'
|
**kwargs
|
Any
|
Extra provider-specific kwargs. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[bytes, str]
|
Tuple of |
Source code in src/codex_ai/core/protocol.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |