teff.harness.formats¶
teff.harness.formats
¶
Response parsing and message-format normalisation for LLM providers.
Functions:
| Name | Description |
|---|---|
extract_content |
Extract the assistant text from a response. |
extract_message |
Normalise response formats to |
extract_usage |
Extract |
normalize_text_tool_calls |
Turn a text-embedded tool call into the structured |
parse_text_tool_call |
Parse a tool call embedded in plain text content. |
extract_content
¶
extract_content(data, provider_type, path='', fallback='')
Extract the assistant text from a response.
path is a dot-separated path into data; otherwise the extraction
follows the wire protocol provider_type (Anthropic content blocks,
Ollama root message).
Source code in teff/harness/formats.py
133 134 135 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 | |
extract_message
¶
extract_message(data)
Normalise response formats to {role, content, tool_calls}.
Handles OpenAI (data["choices"][0]["message"]) and
Ollama (data["message"] at root).
Source code in teff/harness/formats.py
12 13 14 15 16 17 18 19 20 21 22 | |
extract_usage
¶
extract_usage(data)
Extract (prompt_tokens, completion_tokens) from an LLM response.
Handles both OpenAI-style (data["usage"]) and Ollama-style
(data["prompt_eval_count"] / data["eval_count"]) formats.
Source code in teff/harness/formats.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
normalize_text_tool_calls
¶
normalize_text_tool_calls(content, msg, *, seq=0)
Turn a text-embedded tool call into the structured tool_calls list.
When content parses as a single {name, arguments|parameters}
object, returns ([tool_call], msg_with_tool_calls); otherwise
returns ([], msg) unchanged. The generated call_id is derived
from seq + the tool name so it is unique within a run.
Returns:
| Type | Description |
|---|---|
list[dict]
|
A |
dict
|
|
Source code in teff/harness/formats.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
parse_text_tool_call
¶
parse_text_tool_call(content)
Parse a tool call embedded in plain text content.
Local models sometimes emit {"name": "rag", "parameters": {...}}
or {"name": "rag", "arguments": {...}} as text instead of using
the structured tool_calls field. Returns (name, args) if found.
Source code in teff/harness/formats.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |