Skip to content

Commit 1f6c6c3

Browse files
author
Sean Smith
authored
Revert "Add to_dataframe method to BinaryAPIReponse (#56)" (#72)
1 parent f0af9a3 commit 1f6c6c3

File tree

5 files changed

+0
-88
lines changed

5 files changed

+0
-88
lines changed

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ dependencies = [
1414
"anyio>=3.5.0, <5",
1515
"distro>=1.7.0, <2",
1616
"sniffio",
17-
"pandas==2.2.3",
18-
"numpy==2.0.2",
1917
]
2018
requires-python = ">= 3.8"
2119
classifiers = [
@@ -57,8 +55,6 @@ dev-dependencies = [
5755
"importlib-metadata>=6.7.0",
5856
"rich>=13.7.1",
5957
"nest_asyncio==1.6.0",
60-
"pandas==2.2.3",
61-
"numpy==2.0.2",
6258
]
6359

6460
[tool.rye.scripts]

requirements-dev.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,3 @@ virtualenv==20.24.5
102102
# via nox
103103
zipp==3.17.0
104104
# via importlib-metadata
105-
pandas==2.2.3
106-
# via contextual-client
107-
numpy==2.0.2
108-
# via contextual-client

requirements.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,3 @@ typing-extensions==4.12.2
4343
# via contextual-client
4444
# via pydantic
4545
# via pydantic-core
46-
pandas==2.2.3
47-
# via contextual-client
48-
numpy==2.0.2
49-
# via contextual-client

src/contextual/_response.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import ast
5-
import json
64
import inspect
75
import logging
86
import datetime
@@ -25,7 +23,6 @@
2523
import anyio
2624
import httpx
2725
import pydantic
28-
from pandas import DataFrame # type: ignore[import]
2926

3027
from ._types import NoneType
3128
from ._utils import is_given, extract_type_arg, is_annotated_type, is_type_alias_type, extract_type_var_from_base
@@ -482,61 +479,6 @@ class BinaryAPIResponse(APIResponse[bytes]):
482479
the API request, e.g. `.with_streaming_response.get_binary_response()`
483480
"""
484481

485-
def to_dataframe(self) -> DataFrame:
486-
"""Convert the response data to a pandas DataFrame.
487-
488-
Note: This method requires the `pandas` library to be installed.
489-
490-
Returns:
491-
DataFrame: Processed evaluation data
492-
"""
493-
# Read the binary content
494-
content = self.read()
495-
496-
# Now decode the content
497-
lines = content.decode("utf-8").strip().split("\n")
498-
499-
# Parse each line and flatten the results
500-
data = []
501-
for line in lines:
502-
try:
503-
entry = json.loads(line)
504-
# Parse the results field directly from JSON
505-
if 'results' in entry:
506-
if isinstance(entry['results'], str):
507-
# Try to handle string representations that are valid JSON
508-
try:
509-
results = json.loads(entry['results'])
510-
except Exception as e:
511-
# If not valid JSON, fall back to safer processing
512-
results = ast.literal_eval(entry['results'])
513-
else:
514-
# Already a dictionary
515-
results = entry['results']
516-
517-
# Remove the original results field
518-
del entry['results']
519-
# Flatten the nested dictionary structure
520-
if isinstance(results, dict):
521-
for key, value in results.items(): # type: ignore
522-
if isinstance(value, dict):
523-
for subkey, subvalue in value.items(): # type: ignore
524-
if isinstance(subvalue, dict):
525-
# Handle one more level of nesting
526-
for subsubkey, subsubvalue in subvalue.items(): # type: ignore
527-
entry[f'{key}_{subkey}_{subsubkey}'] = subsubvalue
528-
else:
529-
entry[f'{key}_{subkey}'] = subvalue
530-
else:
531-
entry[key] = value
532-
533-
data.append(entry) # type: ignore
534-
except Exception as e:
535-
log.error(f"Error processing line: {e}")
536-
log.error(f"Problematic line: {line[:200]}...") # Print first 200 chars of the line
537-
continue
538-
return DataFrame(data)
539-
540482
def write_to_file(
541483
self,
542484
file: str | os.PathLike[str],

tests/test_response.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,6 @@ def test_response_parse_mismatched_basemodel(client: ContextualAI) -> None:
7373
response.parse(to=PydanticModel)
7474

7575

76-
def test_response_binary_response_to_dataframe(client: ContextualAI) -> None:
77-
response = BinaryAPIResponse(
78-
raw=httpx.Response(
79-
200,
80-
content=b'{"prompt": "What was Apple\'s total net sales for 2022?", "reference": "...", "response": "...", "guideline": "", "knowledge": "[]", "results": "{\'equivalence_score\': {\'score\': 0.0, \'metadata\': \\"The generated response does not provide any information about Apple\'s total net sales for 2022, whereas the reference response provides the specific figure.\\"}, \'factuality_v4.5_score\': {\'score\': 0.0, \'metadata\': {\'description\': \'There are claims but no knowledge so response is ungrounded.\'}}}", "status": "completed"}\r\n',
81-
),
82-
client=client,
83-
stream=False,
84-
stream_cls=None,
85-
cast_to=bytes,
86-
options=FinalRequestOptions.construct(method="get", url="/foo"),
87-
)
88-
df = response.to_dataframe()
89-
assert df.shape == (1, 10)
90-
assert df["prompt"].astype(str).iloc[0] == "What was Apple's total net sales for 2022?" # type: ignore
91-
assert df["equivalence_score_score"].astype(float).iloc[0] == 0.0 # type: ignore
92-
93-
9476
@pytest.mark.asyncio
9577
async def test_async_response_parse_mismatched_basemodel(async_client: AsyncContextualAI) -> None:
9678
response = AsyncAPIResponse(

0 commit comments

Comments
 (0)