Skip to content

Commit 0155d06

Browse files
authored
refactor(together): Drop instructor and use native response_format … (#454)
…instead. According to https://docs.together.ai/docs/json-mode
1 parent 9b0a165 commit 0155d06

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ watsonx = [
7070

7171
together = [
7272
"together",
73-
"instructor",
7473
]
7574

7675
sambanova = [

src/any_llm/providers/together/together.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from typing import TYPE_CHECKING, Any, cast
44

5+
from pydantic import BaseModel
6+
57
from any_llm.any_llm import AnyLLM
6-
from any_llm.utils.instructor import _convert_instructor_response
78

89
MISSING_PACKAGES_ERROR = None
910
try:
10-
import instructor
1111
import together
1212

1313
from .utils import (
@@ -55,10 +55,19 @@ class TogetherProvider(AnyLLM):
5555
@staticmethod
5656
def _convert_completion_params(params: CompletionParams, **kwargs: Any) -> dict[str, Any]:
5757
"""Convert CompletionParams to kwargs for Together API."""
58-
# Together does not support providing reasoning effort
5958
converted_params = params.model_dump(exclude_none=True, exclude={"model_id", "messages", "response_format"})
6059
if converted_params.get("reasoning_effort") == "auto":
6160
converted_params.pop("reasoning_effort")
61+
if (
62+
params.response_format is not None
63+
and isinstance(params.response_format, type)
64+
and issubclass(params.response_format, BaseModel)
65+
):
66+
converted_params["response_format"] = {
67+
"type": "json_schema",
68+
"schema": params.response_format.model_json_schema(),
69+
}
70+
6271
converted_params.update(kwargs)
6372
return converted_params
6473

@@ -124,24 +133,6 @@ async def _acompletion(
124133
params: CompletionParams,
125134
**kwargs: Any,
126135
) -> ChatCompletion | AsyncIterator[ChatCompletionChunk]:
127-
"""Make the API call to Together AI with instructor support for structured outputs."""
128-
129-
if params.response_format:
130-
instructor_client = instructor.patch(self.client, mode=instructor.Mode.JSON) # type: ignore [call-overload]
131-
132-
instructor_response = await instructor_client.chat.completions.create(
133-
model=params.model_id,
134-
messages=cast("Any", params.messages),
135-
response_model=params.response_format,
136-
**params.model_dump(
137-
exclude_none=True,
138-
exclude={"model_id", "messages", "reasoning_effort", "response_format"},
139-
),
140-
**kwargs,
141-
)
142-
143-
return _convert_instructor_response(instructor_response, params.model_id, self.PROVIDER_NAME)
144-
145136
completion_kwargs = self._convert_completion_params(params, **kwargs)
146137

147138
if params.stream:

0 commit comments

Comments
 (0)