Skip to content

Commit 8fb9c83

Browse files
polish: making chat_buffers and chat_wrappers type safe.
1 parent a3faa51 commit 8fb9c83

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/chat_buffers.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,52 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
17+
from openai.types.chat.chat_completion_chunk import ChoiceDeltaToolCall
1518

16-
class ToolCallBuffer:
17-
def __init__(self, index, tool_call_id, function_name):
18-
self.index = index
19-
self.function_name = function_name
20-
self.tool_call_id = tool_call_id
21-
self.arguments = []
2219

23-
def append_arguments(self, arguments):
20+
class ToolCallBuffer:
21+
def __init__(
22+
self,
23+
index: int,
24+
tool_call_id: str | None,
25+
function_name: str | None,
26+
) -> None:
27+
self.index: int = index
28+
self.function_name: str | None = function_name
29+
self.tool_call_id: str | None = tool_call_id
30+
self.arguments: list[str] = []
31+
32+
def append_arguments(self, arguments: str | None) -> None:
2433
if arguments is not None:
2534
self.arguments.append(arguments)
2635

2736

2837
class ChoiceBuffer:
29-
def __init__(self, index):
30-
self.index = index
31-
self.finish_reason = None
32-
self.text_content = []
33-
self.tool_calls_buffers = []
38+
def __init__(self, index: int) -> None:
39+
self.index: int = index
40+
self.finish_reason: str | None = None
41+
self.text_content: list[str] = []
42+
self.tool_calls_buffers: list[ToolCallBuffer | None] = []
3443

35-
def append_text_content(self, content):
44+
def append_text_content(self, content: str) -> None:
3645
self.text_content.append(content)
3746

38-
def append_tool_call(self, tool_call):
47+
def append_tool_call(self, tool_call: ChoiceDeltaToolCall) -> None:
3948
idx = tool_call.index
4049
for _ in range(len(self.tool_calls_buffers), idx + 1):
4150
self.tool_calls_buffers.append(None)
4251

4352
function = tool_call.function
44-
if not self.tool_calls_buffers[idx]:
45-
self.tool_calls_buffers[idx] = ToolCallBuffer(
53+
buffer = self.tool_calls_buffers[idx]
54+
if buffer is None:
55+
buffer = ToolCallBuffer(
4656
idx,
4757
tool_call.id,
4858
function.name if function else None,
4959
)
60+
self.tool_calls_buffers[idx] = buffer
5061

5162
if function:
52-
self.tool_calls_buffers[idx].append_arguments(function.arguments)
63+
buffer.append_arguments(function.arguments)

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/chat_wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _set_output_messages(self):
125125
)
126126
if choice.tool_calls_buffers:
127127
tool_calls = []
128-
for tool_call in choice.tool_calls_buffers:
128+
for tool_call in filter(None, choice.tool_calls_buffers):
129129
arguments = None
130130
arguments_str = "".join(tool_call.arguments)
131131
if arguments_str:

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def cleanup(self, error: Optional[BaseException] = None):
770770
message["content"] = "".join(choice.text_content)
771771
if choice.tool_calls_buffers:
772772
tool_calls = []
773-
for tool_call in choice.tool_calls_buffers:
773+
for tool_call in filter(None, choice.tool_calls_buffers):
774774
function = {"name": tool_call.function_name}
775775
if self.capture_content:
776776
function["arguments"] = "".join(tool_call.arguments)

0 commit comments

Comments
 (0)