Skip to content

Commit 65cc8b2

Browse files
committed
fix(openai): Tool name or arguments might be empty/null
Some providers might return empty/null values in streaming delta (I guess it's to keep schema consistent?). For example, streaming data for a request might be: data: { ... "choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[{"index":0, ... "function":{"name":"get_database_schema","arguments":""}}]}}] ... } data: { ... "choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"name":null,"arguments":"{}"}}]}}] ... } data: { ... "choices":[{"index":0,"finish_reason":"tool_calls","delta":{}}] ... } data: [DONE] Then pgAdmin will combine the delta to a tool call with name "null" and arguments "{}", which leads to a error that can't find the tool. Though I don't find the official doc that mentions we should skip null value in delta, the official openai python sdk skips null values [1]. This commit is to skip null values in streaming delta to avoid empty/null tool name or arguments. [1] https://github.com/openai/openai-python/blob/58184ad545ee2abd98e171ee09766f259d7f38cd/src/openai/lib/streaming/_deltas.py#L6 Signed-off-by: Adam Tao <tcx4c70@gmail.com>
1 parent d59fcf3 commit 65cc8b2

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

web/pgadmin/llm/providers/openai.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,9 @@ def _read_openai_stream(
783783
if 'id' in tc_delta:
784784
tc['id'] = tc_delta['id']
785785
func = tc_delta.get('function', {})
786-
if 'name' in func:
786+
if 'name' in func and func['name']:
787787
tc['name'] = func['name']
788-
if 'arguments' in func:
788+
if 'arguments' in func and func['arguments']:
789789
tc['arguments'] += func['arguments']
790790

791791
# Build final response

0 commit comments

Comments
 (0)