libs/partners/fireworks/langchain_fireworks/chat_models.py PYTHON 2,071 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 2,071.
1"""Fireworks chat wrapper."""23from __future__ import annotations45import contextlib6import json7import logging8from collections.abc import AsyncIterator, Callable, Iterator, Mapping, Sequence9from contextvars import ContextVar10from operator import itemgetter11from typing import (12    Any,13    Literal,14    NoReturn,15    TypeAlias,16    cast,17)1819import httpx20from fireworks import (21    APIConnectionError,22    APIError,23    APITimeoutError,24    AsyncFireworks,25    AuthenticationError,26    BadRequestError,27    Fireworks,28    FireworksError,29    InternalServerError,30    NotFoundError,31    PermissionDeniedError,32    RateLimitError,33)34from langchain_core.callbacks import (35    AsyncCallbackManagerForLLMRun,36    CallbackManagerForLLMRun,37)38from langchain_core.exceptions import (39    ContextOverflowError,40    ModelAPIError,41    ModelAuthenticationError,42    ModelConnectionError,43    ModelInvalidRequestError,44    ModelNotFoundError,45    ModelPermissionDeniedError,46    ModelRateLimitError,47    ModelTimeoutError,48)49from langchain_core.language_models import (50    LanguageModelInput,51    ModelProfile,52    ModelProfileRegistry,53)54from langchain_core.language_models.chat_models import (55    BaseChatModel,56    LangSmithParams,57    agenerate_from_stream,58    generate_from_stream,59)60from langchain_core.language_models.llms import create_base_retry_decorator61from langchain_core.messages import (62    AIMessage,63    AIMessageChunk,64    BaseMessage,65    BaseMessageChunk,66    ChatMessage,67    ChatMessageChunk,68    FunctionMessage,69    FunctionMessageChunk,70    HumanMessage,71    HumanMessageChunk,72    InvalidToolCall,73    SystemMessage,74    SystemMessageChunk,75    ToolCall,76    ToolMessage,77    ToolMessageChunk,78    UsageMetadata,79    is_data_content_block,80)81from langchain_core.messages.block_translators.openai import (82    convert_to_openai_data_block,83)84from langchain_core.messages.tool import (85    ToolCallChunk,86)87from langchain_core.messages.tool import (88    tool_call_chunk as create_tool_call_chunk,89)90from langchain_core.output_parsers import JsonOutputParser, PydanticOutputParser91from langchain_core.output_parsers.base import OutputParserLike92from langchain_core.output_parsers.openai_tools import (93    JsonOutputKeyToolsParser,94    PydanticToolsParser,95    make_invalid_tool_call,96    parse_tool_call,97)98from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult99from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough100from langchain_core.tools import BaseTool101from langchain_core.utils import (102    get_pydantic_field_names,103)104from langchain_core.utils._gateway import _apply_gateway_config105from langchain_core.utils.function_calling import (106    convert_to_json_schema,107    convert_to_openai_tool,108)109from langchain_core.utils.pydantic import is_basemodel_subclass110from langchain_core.utils.utils import _build_model_kwargs111from pydantic import (112    BaseModel,113    ConfigDict,114    Field,115    PrivateAttr,116    SecretStr,117    model_validator,118)119from typing_extensions import Self120121from langchain_fireworks._compat import _convert_from_v1_to_chat_completions122from langchain_fireworks._version import __version__123from langchain_fireworks.data._profiles import _PROFILES124125logger = logging.getLogger(__name__)126127_PROMPT_CACHE_AFFINITY: ContextVar[str | None] = ContextVar(128    "fireworks_prompt_cache_affinity", default=None129)130131132_MODEL_PROFILES = cast("ModelProfileRegistry", _PROFILES)133134135def _get_default_model_profile(model_name: str) -> ModelProfile:136    default = _MODEL_PROFILES.get(model_name) or {}137    return default.copy()138139140def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:141    """Convert a dictionary to a LangChain message.142143    Args:144        _dict: The dictionary.145146    Returns:147        The LangChain message.148149    """150    role = _dict.get("role")151    if role == "user":152        return HumanMessage(content=_dict.get("content", ""))153    if role == "assistant":154        # Fix for azure155        # Also Fireworks returns None for tool invocations156        content = _dict.get("content", "") or ""157        additional_kwargs: dict = {}158        if (159            isinstance(reasoning_content := _dict.get("reasoning_content"), str)160            and reasoning_content161        ):162            additional_kwargs["reasoning_content"] = reasoning_content163164        if function_call := _dict.get("function_call"):165            additional_kwargs["function_call"] = dict(function_call)166167        tool_calls = []168        invalid_tool_calls = []169        if raw_tool_calls := _dict.get("tool_calls"):170            additional_kwargs["tool_calls"] = raw_tool_calls171            for raw_tool_call in raw_tool_calls:172                try:173                    tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))174                except Exception as e:175                    invalid_tool_calls.append(176                        dict(make_invalid_tool_call(raw_tool_call, str(e)))177                    )178        return AIMessage(179            content=content,180            additional_kwargs=additional_kwargs,181            tool_calls=tool_calls,182            invalid_tool_calls=invalid_tool_calls,183        )184    if role == "system":185        return SystemMessage(content=_dict.get("content", ""))186    if role == "function":187        return FunctionMessage(188            content=_dict.get("content", ""), name=_dict.get("name", "")189        )190    if role == "tool":191        additional_kwargs = {}192        if "name" in _dict:193            additional_kwargs["name"] = _dict["name"]194        return ToolMessage(195            content=_dict.get("content", ""),196            tool_call_id=_dict.get("tool_call_id", ""),197            additional_kwargs=additional_kwargs,198        )199    return ChatMessage(content=_dict.get("content", ""), role=role or "")200201202def _allowed_content_part_keys() -> frozenset[str]:203    """Allowlist of wire-valid keys on a Fireworks content part.204205    Derived at import time from the stainless-generated TypedDict so the206    allowlist tracks the upstream OpenAPI spec as `fireworks-ai` is bumped:207    new fields widen the allowlist for free, removed/renamed fields shrink it208    in lockstep. If the SDK reshuffles its module layout the import falls back209    to a conservative hand-coded set and emits a warning, and the layout test210    (`test_fireworks_sdk_request_layout_stable`) fails to surface the drift.211    """212    try:213        from typing import get_type_hints214215        from fireworks.types.shared_params.chat_message import (216            ContentUnionMember1,217        )218219        return frozenset(get_type_hints(ContentUnionMember1))220    except ImportError:221        logger.warning(222            "Could not import `fireworks.types.shared_params.chat_message."223            "ContentUnionMember1`; falling back to a conservative content-part "224            "key allowlist. Bump `fireworks-ai` or update "225            "`_allowed_content_part_keys` if the SDK has moved this type.",226        )227        return frozenset({"type", "text", "image_url", "video_url"})228229230_ALLOWED_CONTENT_PART_KEYS: frozenset[str] = _allowed_content_part_keys()231232233_DROPPED_CONTENT_BLOCK_TYPES: frozenset[str] = frozenset(234    {235        "tool_use",236        "thinking",237        "reasoning",238        "reasoning_content",239        "function_call",240        "code_interpreter_call",241    }242)243"""Content block types the chat completions wire format does not carry.244245These arise from provider-specific or canonical v1 content (e.g. Anthropic246`tool_use`/`thinking` blocks, or a `reasoning` block on an AIMessage) that247reaches `_convert_message_to_dict` as conversation history. Fireworks rejects248them, so they are dropped rather than forwarded.249"""250251252def _sanitize_chat_completions_content(content: Any) -> Any:253    """Strip non-wire keys from content blocks before serializing to Fireworks.254255    Fireworks's chat completions endpoint rejects unknown fields on message256    content parts with `Extra inputs are not permitted, field: 'messages[N]257    .content.list[ChatMessageContent][i].<key>'`. This surfaces when a258    conversation accumulates AIMessages from a different provider (e.g.259    Anthropic's v1 streaming-reassembly `index` marker on text blocks, or the260    LangChain-internal `caller` key on `tool_use` blocks) and that history is261    later forwarded to a Fireworks-hosted model.262263    For list content:264        - each block dict is filtered down to keys in265            `_ALLOWED_CONTENT_PART_KEYS` (sourced from the SDK TypedDict, so it266            stays in sync with the upstream spec).267        - if the result is a list of exactly one block that, post-strip, is268            `{"type": "text", "text": <str>}` and nothing else, it is coerced to269            a plain string. Fireworks's `content` union lists `str` first270            (`Input should be a valid string, field: 'messages[N].content.str'`),271            and the stricter shape avoids the union-validation noise on the272            server side.273    Non-list content (strings, None) passes through unchanged.274    """275    if not isinstance(content, list):276        return content277    sanitized: list[Any] = []278    for block in content:279        if isinstance(block, dict):280            sanitized.append(281                {k: v for k, v in block.items() if k in _ALLOWED_CONTENT_PART_KEYS}282            )283        else:284            sanitized.append(block)285    if (286        len(sanitized) == 1287        and isinstance(sanitized[0], dict)288        and set(sanitized[0]) == {"type", "text"}289        and sanitized[0]["type"] == "text"290        and isinstance(sanitized[0]["text"], str)291    ):292        return sanitized[0]["text"]293    return sanitized294295296def _format_message_content(content: Any) -> Any:297    """Format message content for the Fireworks chat completions wire format.298299    Adapted from `langchain_openai.chat_models.base._format_message_content`,300    scoped to the chat completions API: drops content block types the wire301    format does not carry, translates canonical v0/v1 multimodal data blocks302    via `convert_to_openai_data_block(block, api="chat/completions")`, and303    converts legacy Anthropic-shape image blocks (`{"type": "image",304    "source": {...}}`) to OpenAI `image_url` blocks. String and non-list305    content are returned unchanged.306307    Args:308        content: The message content. Strings and non-list values are309            returned as-is; lists are walked block by block.310311    Returns:312        The formatted content, ready to be placed on the chat completions313        wire. List inputs return a new list with translations applied; other314        inputs are returned unchanged.315    """316    if not isinstance(content, list):317        return content318    formatted: list[Any] = []319    for block in content:320        if isinstance(block, dict) and "type" in block:321            btype = block["type"]322            if btype in _DROPPED_CONTENT_BLOCK_TYPES:323                continue324            if is_data_content_block(block):325                formatted.append(326                    convert_to_openai_data_block(block, api="chat/completions")327                )328                continue329            if (330                btype == "image"331                and (source := block.get("source"))332                and isinstance(source, dict)333            ):334                if (335                    source.get("type") == "base64"336                    and (media_type := source.get("media_type"))337                    and (data := source.get("data"))338                ):339                    formatted.append(340                        {341                            "type": "image_url",342                            "image_url": {"url": f"data:{media_type};base64,{data}"},343                        }344                    )345                    continue346                if source.get("type") == "url" and (url := source.get("url")):347                    formatted.append({"type": "image_url", "image_url": {"url": url}})348                    continue349                continue350        formatted.append(block)351    return formatted352353354def _format_tool_call_arguments(arguments: str | dict | None) -> str | dict:355    """Preserve invalid historical arguments inside a JSON object for replay."""356    if isinstance(arguments, dict):357        return arguments358    try:359        parsed = json.loads(arguments) if arguments is not None else None360        if isinstance(parsed, dict) and arguments is not None:361            json.dumps(parsed, allow_nan=False)362            return arguments363    except ValueError:364        logger.debug("Invalid JSON in historical Fireworks tool call arguments")365    logger.warning("Wrapping invalid historical Fireworks tool call arguments")366    return json.dumps({"__invalid_tool_call_arguments": arguments}, ensure_ascii=False)367368369def _convert_message_to_dict(message: BaseMessage) -> dict:370    """Convert a LangChain message to a dictionary.371372    Args:373        message: The LangChain message.374375    Returns:376        The dictionary.377378    """379    message_dict: dict[str, Any]380    if isinstance(message, ChatMessage):381        message_dict = {382            "role": message.role,383            "content": _sanitize_chat_completions_content(384                _format_message_content(message.content)385            ),386        }387    elif isinstance(message, HumanMessage):388        message_dict = {389            "role": "user",390            "content": _sanitize_chat_completions_content(391                _format_message_content(message.content)392            ),393        }394    elif isinstance(message, AIMessage):395        # Translate v1 content396        if message.response_metadata.get("output_version") == "v1":397            message = _convert_from_v1_to_chat_completions(message)398        message_dict = {399            "role": "assistant",400            "content": _sanitize_chat_completions_content(401                _format_message_content(message.content)402            ),403        }404        # Replay reasoning only when the message is tagged `fireworks` or has no405        # `model_provider` (hand-built messages, or integrations that don't tag406        # theirs); reasoning from other tagged providers may not be valid input.407        if isinstance(408            reasoning_content := message.additional_kwargs.get("reasoning_content"), str409        ) and message.response_metadata.get("model_provider") in (None, "fireworks"):410            message_dict["reasoning_content"] = reasoning_content411        if "function_call" in message.additional_kwargs:412            message_dict["function_call"] = message.additional_kwargs["function_call"]413            # If function call only, content is None not empty string414            if message_dict["content"] == "":415                message_dict["content"] = None416        if message.tool_calls or message.invalid_tool_calls:417            message_dict["tool_calls"] = [418                _lc_tool_call_to_fireworks_tool_call(tc) for tc in message.tool_calls419            ] + [420                _lc_invalid_tool_call_to_fireworks_tool_call(tc)421                for tc in message.invalid_tool_calls422            ]423        elif "tool_calls" in message.additional_kwargs:424            message_dict["tool_calls"] = message.additional_kwargs["tool_calls"]425        if "tool_calls" in message_dict:426            message_dict["tool_calls"] = [427                {428                    **tool_call,429                    "function": {430                        **tool_call["function"],431                        "arguments": _format_tool_call_arguments(432                            tool_call["function"]["arguments"]433                        ),434                    },435                }436                for tool_call in message_dict["tool_calls"]437            ]438        # If tool calls only, content is None not empty string439        if "tool_calls" in message_dict and message_dict["content"] == "":440            message_dict["content"] = None441        else:442            pass443    elif isinstance(message, SystemMessage):444        message_dict = {445            "role": "system",446            "content": _sanitize_chat_completions_content(447                _format_message_content(message.content)448            ),449        }450    elif isinstance(message, FunctionMessage):451        message_dict = {452            "role": "function",453            "content": message.content,454            "name": message.name,455        }456    elif isinstance(message, ToolMessage):457        message_dict = {458            "role": "tool",459            "content": _sanitize_chat_completions_content(460                _format_message_content(message.content)461            ),462            "tool_call_id": message.tool_call_id,463        }464    else:465        msg = f"Got unknown type {message}"466        raise TypeError(msg)467    if "name" in message.additional_kwargs:468        message_dict["name"] = message.additional_kwargs["name"]469    return message_dict470471472def _usage_to_metadata(usage: Mapping[str, Any]) -> UsageMetadata:473    input_tokens = usage.get("prompt_tokens") or 0474    output_tokens = usage.get("completion_tokens") or 0475    usage_metadata: UsageMetadata = {476        "input_tokens": input_tokens,477        "output_tokens": output_tokens,478        "total_tokens": usage.get("total_tokens") or input_tokens + output_tokens,479    }480    cached_tokens = (usage.get("prompt_tokens_details") or {}).get("cached_tokens")481    if cached_tokens is not None:482        usage_metadata["input_token_details"] = {"cache_read": cached_tokens}483    reasoning_tokens = (usage.get("completion_tokens_details") or {}).get(484        "reasoning_tokens"485    )486    if reasoning_tokens is not None:487        usage_metadata["output_token_details"] = {"reasoning": reasoning_tokens}488    return usage_metadata489490491TokenUsageTree: TypeAlias = "int | dict[str, TokenUsageTree]"492"""Raw provider token usage: a tree of `int` leaves and nested `dict` nodes493(e.g. `prompt_tokens_details`).494495Modeled as a recursive alias so the merge helper's signature carries the shape496rather than leaving it to `Any`.497"""498499500def _update_token_usage(501    overall_token_usage: TokenUsageTree, new_usage: TokenUsageTree502) -> TokenUsageTree:503    """Recursively merge raw provider token usage across generations.504505    Token usage is a tree of `int` leaves (summed) and `dict` nodes such as506    `prompt_tokens_details` (merged key-by-key, skipping `None` values).507508    A type mismatch between the accumulator and the incoming value (e.g. an509    `int` on one side and a `dict` on the other) indicates malformed provider510    data and is raised rather than silently coerced. An entirely unexpected511    leaf type (neither `int` nor `dict`) is logged and passed through, so a512    telemetry anomaly degrades gracefully instead of failing the response.513    """514    if isinstance(new_usage, int):515        if not isinstance(overall_token_usage, int):516            msg = (517                "Got different types for token usage: "518                f"{new_usage!r} ({type(new_usage).__name__}) and "519                f"{overall_token_usage!r} ({type(overall_token_usage).__name__})"520            )521            raise ValueError(msg)522        return overall_token_usage + new_usage523    if isinstance(new_usage, dict):524        if not isinstance(overall_token_usage, dict):525            msg = (526                "Got different types for token usage: "527                f"{new_usage!r} ({type(new_usage).__name__}) and "528                f"{overall_token_usage!r} ({type(overall_token_usage).__name__})"529            )530            raise ValueError(msg)531        updated_token_usage = dict(overall_token_usage)532        for key, value in new_usage.items():533            if value is not None:534                # Seed a first-seen key with an empty node of the same kind so a535                # nested `dict` value merges rather than colliding with an `int`.536                default: TokenUsageTree = {} if isinstance(value, dict) else 0537                updated_token_usage[key] = _update_token_usage(538                    overall_token_usage.get(key, default), value539                )540        return updated_token_usage541    logger.warning("Unexpected type for token usage: %s", type(new_usage).__name__)542    return new_usage543544545def _convert_chunk_to_message_chunk(546    chunk: Mapping[str, Any], default_class: type[BaseMessageChunk]547) -> BaseMessageChunk:548    choices = chunk.get("choices") or []549    response_metadata: dict[str, Any] = {"model_provider": "fireworks"}550    if not choices:551        # Final chunk emitted when `stream_options.include_usage=True`:552        # `choices` is empty and the chunk carries only `usage`.553        usage = chunk.get("usage")554        if not usage:555            logger.debug(556                "Received stream chunk with no choices and no usage: %s", chunk557            )558        usage_metadata = _usage_to_metadata(usage) if usage else None559        return AIMessageChunk(560            content="",561            usage_metadata=usage_metadata,562            response_metadata=response_metadata,563        )564    choice = choices[0]565    _dict = choice["delta"]566    role = cast(str, _dict.get("role"))567    content = cast(str, _dict.get("content") or "")568    additional_kwargs: dict = {}569    tool_call_chunks: list[ToolCallChunk] = []570    if (571        isinstance(reasoning_content := _dict.get("reasoning_content"), str)572        and reasoning_content573    ):574        additional_kwargs["reasoning_content"] = reasoning_content575    if _dict.get("function_call"):576        function_call = dict(_dict["function_call"])577        if "name" in function_call and function_call["name"] is None:578            function_call["name"] = ""579        additional_kwargs["function_call"] = function_call580    if raw_tool_calls := _dict.get("tool_calls"):581        additional_kwargs["tool_calls"] = raw_tool_calls582        for rtc in raw_tool_calls:583            with contextlib.suppress(KeyError):584                tool_call_chunks.append(585                    create_tool_call_chunk(586                        name=rtc["function"].get("name"),587                        args=rtc["function"].get("arguments"),588                        id=rtc.get("id"),589                        index=rtc.get("index"),590                    )591                )592    if role == "user" or default_class == HumanMessageChunk:593        return HumanMessageChunk(content=content)594    if role == "assistant" or default_class == AIMessageChunk:595        usage = chunk.get("usage")596        usage_metadata = _usage_to_metadata(usage) if usage else None597        return AIMessageChunk(598            content=content,599            additional_kwargs=additional_kwargs,600            tool_call_chunks=tool_call_chunks,601            usage_metadata=usage_metadata,602            response_metadata=response_metadata,603        )604    if role == "system" or default_class == SystemMessageChunk:605        return SystemMessageChunk(content=content)606    if role == "function" or default_class == FunctionMessageChunk:607        return FunctionMessageChunk(content=content, name=_dict["name"])608    if role == "tool" or default_class == ToolMessageChunk:609        return ToolMessageChunk(content=content, tool_call_id=_dict["tool_call_id"])610    if role or default_class == ChatMessageChunk:611        return ChatMessageChunk(content=content, role=role)612    return default_class(content=content)  # type: ignore[call-arg]613614615def _format_chunk_for_v1(message_chunk: AIMessageChunk) -> AIMessageChunk:616    """Convert a stream chunk to v1 content blocks with separate index namespaces.617618    Fireworks streams a single text field and a single `reasoning_content` field,619    but numbers tool calls from 0. Left to core, streaming (`stream`, `astream`,620    and streaming `invoke`) gives unindexed blocks an integer that increments each621    time the block type changes, so tool call 0 would be merged into the reasoning622    block by `merge_lists`, and reasoning interrupted by text would split in two.623624    `merge_lists` merges blocks with matching indices only when the index is an int625    or a string starting with `lc_`; other strings are appended unmerged. The raw626    `tool_call_chunks` keep the provider's integer indices, which627    `AIMessageChunk.__add__` relies on to merge tool-call arguments.628    """629    blocks: list[dict[str, Any]] = []630    if reasoning := message_chunk.additional_kwargs.get("reasoning_content"):631        blocks.append(632            {"type": "reasoning", "reasoning": reasoning, "index": "lc_reasoning"}633        )634    if message_chunk.content:635        blocks.append(636            {"type": "text", "text": message_chunk.content, "index": "lc_text"}637        )638    for tool_call_chunk in message_chunk.tool_call_chunks:639        block: dict[str, Any] = {640            "type": "tool_call_chunk",641            "id": tool_call_chunk["id"],642            "name": tool_call_chunk["name"],643            "args": tool_call_chunk["args"],644        }645        if (index := tool_call_chunk["index"]) is not None:646            block["index"] = f"lc_tc_{index}"647        blocks.append(block)648    return message_chunk.model_copy(649        update={650            "content": blocks,651            "response_metadata": {652                **message_chunk.response_metadata,653                "output_version": "v1",654            },655        }656    )657658659def _record_usage(previous: UsageMetadata | None, new: UsageMetadata) -> UsageMetadata:660    """Return the newest usage report, logging when it disagrees with an earlier one."""661    if previous is not None and previous != new:662        logger.debug(663            "Fireworks reported differing usage within one stream; using the "664            "latest. Previous: %s, latest: %s",665            previous,666            new,667        )668    return new669670671def _finalize_stream(672    stream: Iterator[ChatGenerationChunk],673) -> Iterator[ChatGenerationChunk]:674    """Supply core's finalization marker when aggregating v1 chunks internally."""675    yield from stream676    yield ChatGenerationChunk(message=AIMessageChunk(content="", chunk_position="last"))677678679async def _afinalize_stream(680    stream: AsyncIterator[ChatGenerationChunk],681) -> AsyncIterator[ChatGenerationChunk]:682    """Supply core's finalization marker when aggregating v1 chunks internally."""683    async for chunk in stream:684        yield chunk685    yield ChatGenerationChunk(message=AIMessageChunk(content="", chunk_position="last"))686687688class _RetryableHTTPStatusError(FireworksError):689    """Internal marker for 5xx `httpx.HTTPStatusError` responses.690691    The 1.x SDK wraps every status response into a typed `APIStatusError`692    subclass, so this path is defense-in-depth: it only fires when a raw693    `httpx.HTTPStatusError` escapes the SDK (e.g., a custom `http_client` or694    monkey-patched transport raises one directly). Promoting it here keeps the695    retryable set expressible as a list of classes for696    `create_base_retry_decorator`.697    """698699700_RETRYABLE_ERRORS: tuple[type[BaseException], ...] = (701    APIConnectionError,702    InternalServerError,703    RateLimitError,704    httpx.TimeoutException,705    httpx.TransportError,706    _RetryableHTTPStatusError,707)708709710def _promote_http_status_error(exc: httpx.HTTPStatusError) -> NoReturn:711    """Re-raise 5xx `httpx.HTTPStatusError` as a retryable marker."""712    if exc.response.status_code >= 500:713        msg = f"Retryable {exc.response.status_code} from Fireworks: {exc}"714        raise _RetryableHTTPStatusError(msg) from exc715    raise exc716717718class FireworksContextOverflowError(BadRequestError, ContextOverflowError):719    """`BadRequestError` raised when input exceeds Fireworks's context limit."""720721722class FireworksAuthenticationError(AuthenticationError, ModelAuthenticationError):723    """Fireworks authentication error classified as a LangChain model error."""724725726class FireworksPermissionDeniedError(PermissionDeniedError, ModelPermissionDeniedError):727    """Fireworks permission error classified as a LangChain model error."""728729730class FireworksInvalidRequestError(BadRequestError, ModelInvalidRequestError):731    """Fireworks bad-request error classified as a LangChain model error."""732733734class FireworksModelNotFoundError(NotFoundError, ModelNotFoundError):735    """Fireworks not-found error classified as a LangChain model error."""736737738class FireworksRateLimitError(RateLimitError, ModelRateLimitError):739    """Fireworks rate-limit error classified as a LangChain model error."""740741742class FireworksAPIError(InternalServerError, ModelAPIError):743    """Fireworks server error classified as a LangChain model error."""744745746class FireworksConnectionError(APIConnectionError, ModelConnectionError):747    """Fireworks connection error classified as a LangChain model error."""748749750class FireworksTimeoutError(APITimeoutError, ModelTimeoutError):751    """Fireworks timeout error classified as a LangChain model error."""752753754class FireworksReadTimeoutError(httpx.ReadTimeout, ModelTimeoutError):755    """Fireworks stream read timeout classified as a retryable model error."""756757758def _handle_stream_read_timeout(error: httpx.ReadTimeout) -> NoReturn:759    try:760        request = error.request761    except RuntimeError:762        request = None763    raise FireworksReadTimeoutError(str(error), request=request) from error764765766def _handle_fireworks_invalid_request(e: BadRequestError) -> NoReturn:767    """Promote prompt-too-long errors to `FireworksContextOverflowError`."""768    if "prompt is too long" in str(e):769        raise FireworksContextOverflowError(770            str(e), response=e.response, body=e.body771        ) from e772    raise FireworksInvalidRequestError(str(e), response=e.response, body=e.body) from e773774775def _handle_fireworks_api_error(e: APIError) -> NoReturn:776    """Re-raise a Fireworks SDK error as its LangChain-classified equivalent."""777    if isinstance(e, AuthenticationError):778        raise FireworksAuthenticationError(779            str(e), response=e.response, body=e.body780        ) from e781    if isinstance(e, PermissionDeniedError):782        raise FireworksPermissionDeniedError(783            str(e), response=e.response, body=e.body784        ) from e785    if isinstance(e, NotFoundError):786        raise FireworksModelNotFoundError(787            str(e), response=e.response, body=e.body788        ) from e789    if isinstance(e, RateLimitError):790        raise FireworksRateLimitError(str(e), response=e.response, body=e.body) from e791    if isinstance(e, InternalServerError):792        raise FireworksAPIError(str(e), response=e.response, body=e.body) from e793    # `APITimeoutError` subclasses `APIConnectionError`, so check it first.794    if isinstance(e, APITimeoutError):795        raise FireworksTimeoutError(e.request) from e796    if isinstance(e, APIConnectionError):797        raise FireworksConnectionError(message=str(e), request=e.request) from e798    raise e799800801def _raise_empty_stream() -> NoReturn:802    """Raise a descriptive error when the SDK returns a zero-chunk stream."""803    msg = "Received empty stream from Fireworks"804    raise FireworksError(msg)805806807def _create_retry_decorator(808    llm: ChatFireworks,809    run_manager: AsyncCallbackManagerForLLMRun | CallbackManagerForLLMRun | None = None,810) -> Callable[[Any], Any]:811    """Return a tenacity retry decorator for Fireworks SDK calls.812813    Retries live here rather than in the SDK so each attempt is visible to the814    LangChain `run_manager.on_retry` callback. The SDK's own retry layer is815    suppressed via `max_retries=0` on the client; see `validate_environment`.816    """817    # `max_retries` counts retries *after* the initial attempt (default lives on818    # the `ChatFireworks.max_retries` field). `create_base_retry_decorator`819    # forwards its `max_retries` to `stop_after_attempt`, which counts total820    # attempts — so offset by 1. `None` and `0` both mean "single attempt, no821    # retries".822    attempts = (llm.max_retries + 1) if llm.max_retries else 1823    return create_base_retry_decorator(824        error_types=list(_RETRYABLE_ERRORS),825        max_retries=attempts,826        run_manager=run_manager,827    )828829830def _prepare_sdk_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:831    """Move fields the 1.x SDK does not model into `extra_body`.832833    The Stainless-generated `chat.completions.create` signature has a fixed set834    of typed parameters. Fireworks accepts additional fields on the wire (notably835    `stream_options.include_usage`) that the SDK schema does not declare. The836    SDK exposes `extra_body` precisely for this — merge anything that looks837    extra-body-shaped into it so it lands in the JSON request body.838839    If a caller supplies both `extra_body={"stream_options": ...}` and a840    top-level `stream_options=...`, the value already in `extra_body` wins841    (callers using `extra_body` are presumed to want explicit control); the842    discarded top-level value is logged.843    """844    extra_body = dict(kwargs.pop("extra_body", None) or {})845    top_level_stream_options = kwargs.pop("stream_options", None)846    if top_level_stream_options is not None:847        if "stream_options" in extra_body:848            logger.warning(849                "Both `extra_body['stream_options']` and a top-level "850                "`stream_options` were supplied; using `extra_body`'s value "851                "and discarding the top-level value.",852            )853        else:854            extra_body["stream_options"] = top_level_stream_options855    if extra_body:856        kwargs["extra_body"] = extra_body857    return kwargs858859860def _merge_model_headers(llm: ChatFireworks, kwargs: dict[str, Any]) -> dict[str, Any]:861    """Merge model-local headers only when invoking their owning model."""862    model_headers = llm.model_kwargs.get("extra_headers")863    request_headers = kwargs.get("extra_headers")864    if isinstance(model_headers, Mapping) and isinstance(request_headers, Mapping):865        # HTTP header names are case-insensitive; retain the request's spelling.866        headers = {867            key.lower(): (key, value)868            for source in (model_headers, request_headers)869            for key, value in source.items()870        }871        return {872            **kwargs,873            "extra_headers": dict(headers.values()),874        }875    return kwargs876877878def _apply_prompt_cache_affinity(kwargs: dict[str, Any]) -> dict[str, Any]:879    """Apply the middleware default after the selected model's settings are merged."""880    affinity = _PROMPT_CACHE_AFFINITY.get()881    if affinity is None:882        return kwargs883884    # The SDK merges extra_body over top-level fields before sending the request.885    extra_body = kwargs.get("extra_body")886    body = {**kwargs, **extra_body} if isinstance(extra_body, Mapping) else kwargs887    if any(body.get(key) for key in ("user", "prompt_cache_key")):888        return kwargs889890    headers = kwargs.get("extra_headers")891    if isinstance(headers, Mapping):892        if any(893            isinstance(key, str) and key.lower() == "x-session-affinity"894            for key in headers895        ):896            return kwargs897    elif headers is not None:898        logger.warning(899            "Cannot set Fireworks session affinity because extra_headers is %s",900            type(headers).__name__,901        )902        return kwargs903904    return {905        **kwargs,906        "prompt_cache_key": affinity,907        "extra_headers": {**(headers or {}), "x-session-affinity": affinity},908    }909910911def _completion_with_retry(912    llm: ChatFireworks,913    run_manager: CallbackManagerForLLMRun | None = None,914    **kwargs: Any,915) -> Any:916    """Retry the sync completion call, including stream setup."""917    retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)918    kwargs = _prepare_sdk_kwargs(919        _apply_prompt_cache_affinity(_merge_model_headers(llm, kwargs))920    )921922    @retry_decorator923    def _call() -> Any:924        try:925            result = llm.client.create(**kwargs)926        except httpx.HTTPStatusError as e:927            _promote_http_status_error(e)928        if kwargs.get("stream"):929            # The streaming generator is lazy — advance once so the HTTP930            # connection and any transport error happen inside the retry931            # boundary. `_prepend_chunk` then re-yields the consumed chunk932            # ahead of the rest so callers still see every event.933            try:934                iterator = iter(result)935                first = next(iterator)936            except StopIteration:937                _raise_empty_stream()938            except httpx.HTTPStatusError as e:939                _promote_http_status_error(e)940            return _prepend_chunk(first, iterator)941        return result942943    return _call()944945946async def _acompletion_with_retry(947    llm: ChatFireworks,948    run_manager: AsyncCallbackManagerForLLMRun | None = None,949    **kwargs: Any,950) -> Any:951    """Retry the async completion call, including stream setup."""952    retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)953    kwargs = _prepare_sdk_kwargs(954        _apply_prompt_cache_affinity(_merge_model_headers(llm, kwargs))955    )956957    @retry_decorator958    async def _call() -> Any:959        if kwargs.get("stream"):960            try:961                # 1.x async `create()` is a coroutine that resolves to an962                # `AsyncStream` when `stream=True`. Await it, then advance the963                # async iterator once inside the retry boundary so transport964                # errors surface here rather than at first downstream consumer.965                result = await llm.async_client.create(**kwargs)966                agen = result.__aiter__()967                first = await agen.__anext__()968            except StopAsyncIteration:969                _raise_empty_stream()970            except httpx.HTTPStatusError as e:971                _promote_http_status_error(e)972            return _aprepend_chunk(first, agen)973        try:974            return await llm.async_client.create(**kwargs)975        except httpx.HTTPStatusError as e:976            _promote_http_status_error(e)977978    return await _call()979980981def _prepend_chunk(first: Any, rest: Iterator[Any]) -> Iterator[Any]:982    yield first983    try:984        yield from rest985    except httpx.ReadTimeout as e:986        _handle_stream_read_timeout(e)987988989async def _aprepend_chunk(first: Any, rest: AsyncIterator[Any]) -> AsyncIterator[Any]:990    yield first991    try:992        async for item in rest:993            yield item994    except httpx.ReadTimeout as e:995        _handle_stream_read_timeout(e)996997998class ChatFireworks(BaseChatModel):999    """`Fireworks` Chat large language models API.10001001    To use, you should have the1002    environment variable `FIREWORKS_API_KEY` set with your API key.10031004    Any parameters that are valid to be passed to the fireworks.create call1005    can be passed in, even if not explicitly saved on this class.10061007    Example:1008        ```python1009        from langchain_fireworks.chat_models import ChatFireworks10101011        model = ChatFireworks(model_name="accounts/fireworks/models/gpt-oss-120b")1012        ```10131014    Fireworks request headers can be passed with `extra_headers`. For prompt1015    caching, `x-session-affinity` pins requests to a replica so related calls can1016    reuse the same prompt-cache session:10171018    ```python1019    model.invoke(1020        "Hello",1021        extra_headers={"x-session-affinity": "user-42"},1022    )1023    ```10241025    The Fireworks SDK also accepts a typed `prompt_cache_key` field (passed as a1026    regular keyword argument), which it treats as the preferred alternative to1027    the raw `x-session-affinity` header:10281029    ```python1030    model.invoke("Hello", prompt_cache_key="user-42")1031    ```1032    """10331034    @property1035    def lc_secrets(self) -> dict[str, str]:1036        return {"fireworks_api_key": "FIREWORKS_API_KEY"}10371038    @classmethod1039    def get_lc_namespace(cls) -> list[str]:1040        """Get the namespace of the LangChain object.10411042        Returns:1043            `["langchain", "chat_models", "fireworks"]`1044        """1045        return ["langchain", "chat_models", "fireworks"]10461047    @property1048    def lc_attributes(self) -> dict[str, Any]:1049        attributes: dict[str, Any] = {}1050        if self.fireworks_api_base:1051            attributes["fireworks_api_base"] = self.fireworks_api_base10521053        return attributes10541055    @classmethod1056    def is_lc_serializable(cls) -> bool:1057        """Return whether this model can be serialized by LangChain."""1058        return True10591060    client: Any = Field(default=None, exclude=True)1061    """Internal `fireworks.Fireworks().chat.completions` resource.10621063    Constructed with `max_retries=0` so retries are owned by1064    `_create_retry_decorator` (which surfaces each attempt to the LangChain1065    `run_manager`). Callers reaching for this directly should set their own1066    retry layer.1067    """10681069    async_client: Any = Field(default=None, exclude=True)1070    """Internal `fireworks.AsyncFireworks().chat.completions` resource.10711072    Constructed with `max_retries=0`; see `client`.1073    """10741075    _sdk_client: Any = PrivateAttr(default=None)1076    """Owning `fireworks.Fireworks` instance, retained so `close()` can call1077    into the underlying HTTPX client. The 1.x SDK does not expose lifecycle1078    methods on the `chat.completions` resource itself.1079    """10801081    _async_sdk_client: Any = PrivateAttr(default=None)1082    """Owning `fireworks.AsyncFireworks` instance; see `_sdk_client`."""10831084    model_name: str = Field(alias="model")1085    """Model name to use."""10861087    @property1088    def model(self) -> str:1089        """Same as model_name."""1090        return self.model_name10911092    temperature: float | None = None1093    """What sampling temperature to use."""10941095    stop: str | list[str] | None = Field(default=None, alias="stop_sequences")1096    """Default stop sequences."""10971098    model_kwargs: dict[str, Any] = Field(default_factory=dict)1099    """Holds any model parameters valid for `create` call not explicitly specified."""11001101    fireworks_api_key: SecretStr = Field(default=SecretStr(""), alias="api_key")1102    """Fireworks API key.11031104    Automatically read from env variable `FIREWORKS_API_KEY` if not provided.11051106    If `LANGSMITH_GATEWAY` is enabled and the base URL points at the gateway,1107    `LANGSMITH_GATEWAY_API_KEY` is used instead.1108    """11091110    fireworks_api_base: str | None = Field(default=None, alias="base_url")1111    """Base URL path for API requests, leave blank if not using a proxy or service1112    emulator.11131114    If `LANGSMITH_GATEWAY` is set, it is used as a fallback after `FIREWORKS_API_BASE`.1115    """11161117    request_timeout: float | tuple[float, float] | Any | None = Field(1118        default=None, alias="timeout"1119    )1120    """Timeout for requests to Fireworks completion API. Can be `float`,1121    `httpx.Timeout` or `None`.1122    """11231124    streaming: bool = False1125    """Whether to stream the results or not."""11261127    stream_usage: bool = True1128    """Whether to include usage metadata in streaming output.11291130    If `True`, a final empty-content chunk carrying `usage_metadata` is emitted1131    during the stream. Set to `False` if the upstream model/proxy rejects1132    `stream_options`, or pass `stream_options` explicitly via `model_kwargs` or1133    a runtime kwarg to override.11341135    !!! version-added "Added in `langchain-fireworks` 1.2.0"11361137    !!! warning "Behavior changed in `langchain-fireworks` 1.2.0"11381139        Streaming now opts into `stream_options.include_usage` by default, and1140        the final empty-`choices` chunk is surfaced as an `AIMessageChunk` with1141        `usage_metadata` instead of being silently dropped.1142    """11431144    n: int = 11145    """Number of chat completions to generate for each prompt."""11461147    max_tokens: int | None = None1148    """Maximum number of tokens to generate."""11491150    max_retries: int | None = 21151    """Maximum number of retries after the initial attempt when generating.11521153    Retries use exponential backoff and trigger on transient errors:1154    `RateLimitError`, `APIConnectionError` (including its `APITimeoutError`1155    subclass), 5xx responses (including those that surface as1156    `httpx.HTTPStatusError` rather than typed SDK errors), and underlying1157    transport errors (`httpx.TimeoutException`, `httpx.TransportError`).1158    A value of `None` or `0` disables retries.1159    """11601161    service_tier: str | None = None1162    """Service tier for the request.11631164    Forwarded as the `service_tier` field on the Fireworks chat completions1165    request when set. Pass `'priority'` to opt into Fireworks' priority tier;1166    leave as `None` to use the default tier.11671168    To use Fireworks' fast mode instead, select a fast-routed `model`; fast mode1169    is not controlled by this field. See Fireworks'1170    [serverless product docs](https://docs.fireworks.ai/guides/serverless-products)1171    for the current list of fast routers and tiers.11721173    !!! version-added "Added in `langchain-fireworks` 1.3.0"1174    """1175    reasoning_effort: str | None = None1176    """Reasoning effort.11771178    Forwarded as the `reasoning_effort` request field. Supported values vary by1179    model; see the model's `profile.reasoning_effort_levels`.11801181    Can also be passed at call time, e.g.1182    `model.invoke(..., reasoning_effort="high")`.1183    """11841185    model_config = ConfigDict(1186        populate_by_name=True,1187    )11881189    @model_validator(mode="before")1190    @classmethod1191    def build_extra(cls, values: dict[str, Any]) -> Any:1192        """Build extra kwargs from additional params that were passed in."""1193        all_required_field_names = get_pydantic_field_names(cls)1194        return _build_model_kwargs(values, all_required_field_names)11951196    @model_validator(mode="before")1197    @classmethod1198    def _resolve_gateway(cls, values: Any) -> Any:1199        """Resolve the base URL and API key, applying LangSmith gateway settings.12001201        An explicit ``base_url``/``api_key`` always wins. Otherwise the base URL1202        falls back to ``FIREWORKS_API_BASE``, then the LangSmith gateway. The1203        gateway key is preferred only when the base URL came from the gateway;1204        for any other endpoint the provider key wins, and the gateway key is a1205        candidate only when the gateway is enabled.1206        """1207        if isinstance(values, dict):1208            config = _apply_gateway_config(1209                values,1210                cls,1211                base_url_field="fireworks_api_base",1212                api_key_field="fireworks_api_key",1213                provider_path="fireworks",1214                base_url_env="FIREWORKS_API_BASE",1215                api_key_env="FIREWORKS_API_KEY",1216            )1217            if config.api_key is None:1218                msg = (1219                    "You must specify an api key. "1220                    "You can pass it an argument as `api_key=...` or "1221                    "set the environment variable `FIREWORKS_API_KEY`."1222                )1223                raise ValueError(msg)1224        return values12251226    @model_validator(mode="after")1227    def _set_fireworks_chat_version(self) -> Self:1228        """Set package version in metadata."""1229        self._add_version("langchain-fireworks", __version__)1230        return self12311232    @model_validator(mode="after")1233    def validate_environment(self) -> Self:1234        """Validate that api key and python package exists in environment."""1235        if self.n < 1:1236            msg = "n must be at least 1."1237            raise ValueError(msg)1238        if self.n > 1 and self.streaming:1239            msg = "n must be 1 when streaming."1240            raise ValueError(msg)12411242        api_key = self.fireworks_api_key.get_secret_value()1243        base_url = self.fireworks_api_base1244        # 0.x accepted a `(connect, read)` tuple. 1.x's SDK only accepts a1245        # float, `httpx.Timeout`, or `None` — normalize so existing user code1246        # keeps working.1247        if isinstance(self.request_timeout, tuple):1248            connect, read = self.request_timeout1249            timeout: Any = httpx.Timeout(read, connect=connect)1250        else:1251            timeout = self.request_timeout1252        # `langchain-fireworks` owns retry/backoff via `_create_retry_decorator`1253        # so the LangChain `run_manager` sees each attempt. Suppress the1254        # SDK's built-in retry layer to avoid double-retrying.1255        if not self.client:1256            self._sdk_client = Fireworks(1257                api_key=api_key,1258                base_url=base_url,1259                timeout=timeout,1260                max_retries=0,1261            )1262            self.client = self._sdk_client.chat.completions1263        if not self.async_client:1264            self._async_sdk_client = AsyncFireworks(1265                api_key=api_key,1266                base_url=base_url,1267                timeout=timeout,1268                max_retries=0,1269            )1270            self.async_client = self._async_sdk_client.chat.completions1271        return self12721273    def close(self) -> None:1274        """Close the underlying sync HTTP client.12751276        After calling, sync invocations on this model will raise. Async1277        invocations remain available until `aclose()` is also called. Safe to1278        call multiple times.1279        """1280        if self._sdk_client is not None:1281            self._sdk_client.close()12821283    async def aclose(self) -> None:1284        """Close the underlying async HTTP client.12851286        Releases the aiohttp-backed connector that the 1.x SDK uses by1287        default. Without this, transient `ChatFireworks` instances can leak1288        an `Unclosed connector` warning at GC if the event loop has already1289        stopped. Safe to call multiple times.1290        """1291        if self._async_sdk_client is not None:1292            await self._async_sdk_client.close()12931294    def _resolve_model_profile(self) -> ModelProfile | None:1295        return _get_default_model_profile(self.model_name) or None12961297    @property1298    def _default_params(self) -> dict[str, Any]:1299        """Get the default parameters for calling Fireworks API."""1300        params = {1301            "model": self.model_name,1302            "stream": self.streaming,1303            "n": self.n,1304            "stop": self.stop,1305            **self.model_kwargs,1306        }1307        if self.temperature is not None:1308            params["temperature"] = self.temperature1309        if self.max_tokens is not None:1310            params["max_tokens"] = self.max_tokens1311        if self.service_tier is not None:1312            params["service_tier"] = self.service_tier1313        if self.reasoning_effort is not None:1314            params["reasoning_effort"] = self.reasoning_effort1315        return params13161317    def _get_ls_params(1318        self, stop: list[str] | None = None, **kwargs: Any1319    ) -> LangSmithParams:1320        """Get standard params for tracing."""1321        params = self._get_invocation_params(stop=stop, **kwargs)1322        ls_params = LangSmithParams(1323            ls_provider="fireworks",1324            ls_model_name=params.get("model", self.model_name),1325            ls_model_type="chat",1326            ls_temperature=params.get("temperature", self.temperature),1327        )1328        if ls_max_tokens := params.get("max_tokens", self.max_tokens):1329            ls_params["ls_max_tokens"] = ls_max_tokens1330        if ls_stop := stop or params.get("stop", None):1331            ls_params["ls_stop"] = ls_stop1332        return ls_params13331334    def _combine_llm_outputs(self, llm_outputs: list[dict | None]) -> dict:1335        overall_token_usage: dict = {}1336        system_fingerprint = None1337        model_name = None1338        for output in llm_outputs:1339            if output is None:1340                # Happens in streaming1341                continue1342            token_usage = output.get("token_usage")1343            if token_usage is not None:1344                for k, v in token_usage.items():1345                    if v is None:1346                        continue1347                    if k in overall_token_usage:1348                        overall_token_usage[k] = _update_token_usage(1349                            overall_token_usage[k], v1350                        )1351                    else:1352                        overall_token_usage[k] = v1353            if system_fingerprint is None:1354                system_fingerprint = output.get("system_fingerprint")1355            if model_name is None:1356                model_name = output.get("model_name") or None1357        combined = {1358            "token_usage": overall_token_usage,1359            "model_name": model_name or self.model_name,1360        }1361        if system_fingerprint:1362            combined["system_fingerprint"] = system_fingerprint1363        return combined13641365    def _usage_generation_chunk(self, usage: UsageMetadata) -> ChatGenerationChunk:1366        """Build the single chunk that carries a stream's token usage."""1367        message_chunk = AIMessageChunk(1368            content="",1369            usage_metadata=usage,1370            response_metadata={"model_provider": "fireworks"},1371        )1372        if self.output_version == "v1":1373            message_chunk = _format_chunk_for_v1(message_chunk)1374        return ChatGenerationChunk(message=message_chunk)13751376    def _stream(1377        self,1378        messages: list[BaseMessage],1379        stop: list[str] | None = None,1380        run_manager: CallbackManagerForLLMRun | None = None,1381        **kwargs: Any,1382    ) -> Iterator[ChatGenerationChunk]:1383        message_dicts, params = self._create_message_dicts(messages, stop)1384        params = {**params, **kwargs, "stream": True}1385        if self.stream_usage and "stream_options" not in params:1386            params["stream_options"] = {"include_usage": True}13871388        default_chunk_class: type[BaseMessageChunk] = AIMessageChunk1389        try:1390            stream = _completion_with_retry(1391                self, run_manager=run_manager, messages=message_dicts, **params1392            )1393        except BadRequestError as e:1394            _handle_fireworks_invalid_request(e)1395        except APIError as e:1396            _handle_fireworks_api_error(e)1397        response_model = None1398        last_usage: UsageMetadata | None = None1399        for chunk in stream:1400            if not isinstance(chunk, dict):1401                chunk = chunk.model_dump()1402            response_model = chunk.get("model") or response_model1403            message_chunk = _convert_chunk_to_message_chunk(chunk, default_chunk_class)1404            if self.output_version == "v1" and isinstance(1405                message_chunk, AIMessageChunk1406            ):1407                message_chunk = _format_chunk_for_v1(message_chunk)1408            if (1409                isinstance(message_chunk, AIMessageChunk)1410                and message_chunk.usage_metadata1411            ):1412                # Fireworks can report usage on more than one chunk (e.g. the1413                # `finish_reason` chunk and a trailing usage-only chunk). Merging1414                # chunks sums usage, so hold it back and emit the last report once.1415                last_usage = _record_usage(last_usage, message_chunk.usage_metadata)1416                message_chunk.usage_metadata = None1417                if not chunk.get("choices"):1418                    continue1419            generation_info: dict[str, Any] = {}1420            logprobs = None1421            if choices := chunk.get("choices"):1422                choice = choices[0]1423                if finish_reason := choice.get("finish_reason"):1424                    generation_info["finish_reason"] = finish_reason1425                    # Emit once: repeated model names concatenate when chunks merge.1426                    generation_info["model_name"] = response_model or self.model_name1427                    if service_tier := chunk.get("service_tier"):1428                        generation_info["service_tier"] = service_tier1429                logprobs = choice.get("logprobs")1430                if logprobs:1431                    generation_info["logprobs"] = logprobs1432            default_chunk_class = message_chunk.__class__1433            generation_chunk = ChatGenerationChunk(1434                message=message_chunk, generation_info=generation_info or None1435            )1436            if run_manager:1437                run_manager.on_llm_new_token(1438                    generation_chunk.text, chunk=generation_chunk, logprobs=logprobs1439                )1440            yield generation_chunk1441        if last_usage:1442            usage_chunk = self._usage_generation_chunk(last_usage)1443            if run_manager:1444                run_manager.on_llm_new_token("", chunk=usage_chunk)1445            yield usage_chunk14461447    def _generate(1448        self,1449        messages: list[BaseMessage],1450        stop: list[str] | None = None,1451        run_manager: CallbackManagerForLLMRun | None = None,1452        stream: bool | None = None,  # noqa: FBT0011453        **kwargs: Any,1454    ) -> ChatResult:1455        should_stream = stream if stream is not None else self.streaming1456        if should_stream:1457            stream_iter = self._stream(1458                messages, stop=stop, run_manager=run_manager, **kwargs1459            )1460            if self.output_version == "v1":1461                stream_iter = _finalize_stream(stream_iter)1462            return generate_from_stream(stream_iter)1463        message_dicts, params = self._create_message_dicts(messages, stop)1464        params = {1465            **params,1466            **({"stream": stream} if stream is not None else {}),1467            **kwargs,1468        }1469        try:1470            response = _completion_with_retry(1471                self, run_manager=run_manager, messages=message_dicts, **params1472            )1473        except BadRequestError as e:1474            _handle_fireworks_invalid_request(e)1475        except APIError as e:1476            _handle_fireworks_api_error(e)1477        return self._create_chat_result(response)14781479    def _create_message_dicts(1480        self, messages: list[BaseMessage], stop: list[str] | None1481    ) -> tuple[list[dict[str, Any]], dict[str, Any]]:1482        params = self._default_params1483        if stop is not None:1484            params["stop"] = stop1485        message_dicts = [_convert_message_to_dict(m) for m in messages]1486        return message_dicts, params14871488    def _create_chat_result(self, response: dict | BaseModel) -> ChatResult:1489        generations = []1490        if not isinstance(response, dict):1491            response = response.model_dump()1492        token_usage = response.get("usage", {})1493        model_name = response.get("model") or self.model_name1494        service_tier = response.get("service_tier")1495        for res in response["choices"]:1496            message = _convert_dict_to_message(res["message"])1497            if isinstance(message, AIMessage):1498                message.response_metadata["model_name"] = model_name1499                if token_usage:1500                    message.usage_metadata = _usage_to_metadata(token_usage)1501                    message.response_metadata["model_provider"] = "fireworks"1502                if service_tier:1503                    message.response_metadata["service_tier"] = service_tier1504            generation_info = {"finish_reason": res.get("finish_reason")}1505            if "logprobs" in res:1506                generation_info["logprobs"] = res["logprobs"]1507            gen = ChatGeneration(1508                message=message,1509                generation_info=generation_info,1510            )1511            generations.append(gen)1512        llm_output = {1513            "token_usage": token_usage,1514            "model_name": model_name,1515            "system_fingerprint": response.get("system_fingerprint", ""),1516        }1517        if service_tier:1518            llm_output["service_tier"] = service_tier1519        return ChatResult(generations=generations, llm_output=llm_output)15201521    async def _astream(1522        self,1523        messages: list[BaseMessage],1524        stop: list[str] | None = None,1525        run_manager: AsyncCallbackManagerForLLMRun | None = None,1526        **kwargs: Any,1527    ) -> AsyncIterator[ChatGenerationChunk]:1528        message_dicts, params = self._create_message_dicts(messages, stop)1529        params = {**params, **kwargs, "stream": True}1530        if self.stream_usage and "stream_options" not in params:1531            params["stream_options"] = {"include_usage": True}15321533        default_chunk_class: type[BaseMessageChunk] = AIMessageChunk1534        try:1535            stream = await _acompletion_with_retry(1536                self, run_manager=run_manager, messages=message_dicts, **params1537            )1538        except BadRequestError as e:1539            _handle_fireworks_invalid_request(e)1540        except APIError as e:1541            _handle_fireworks_api_error(e)1542        response_model = None1543        last_usage: UsageMetadata | None = None1544        async for chunk in stream:1545            if not isinstance(chunk, dict):1546                chunk = chunk.model_dump()1547            response_model = chunk.get("model") or response_model1548            message_chunk = _convert_chunk_to_message_chunk(chunk, default_chunk_class)1549            if self.output_version == "v1" and isinstance(1550                message_chunk, AIMessageChunk1551            ):1552                message_chunk = _format_chunk_for_v1(message_chunk)1553            if (1554                isinstance(message_chunk, AIMessageChunk)1555                and message_chunk.usage_metadata1556            ):1557                # Fireworks can report usage on more than one chunk (e.g. the1558                # `finish_reason` chunk and a trailing usage-only chunk). Merging1559                # chunks sums usage, so hold it back and emit the last report once.1560                last_usage = _record_usage(last_usage, message_chunk.usage_metadata)1561                message_chunk.usage_metadata = None1562                if not chunk.get("choices"):1563                    continue1564            generation_info: dict[str, Any] = {}1565            logprobs = None1566            if choices := chunk.get("choices"):1567                choice = choices[0]1568                if finish_reason := choice.get("finish_reason"):1569                    generation_info["finish_reason"] = finish_reason1570                    # Emit once: repeated model names concatenate when chunks merge.1571                    generation_info["model_name"] = response_model or self.model_name1572                    if service_tier := chunk.get("service_tier"):1573                        generation_info["service_tier"] = service_tier1574                logprobs = choice.get("logprobs")1575                if logprobs:1576                    generation_info["logprobs"] = logprobs1577            default_chunk_class = message_chunk.__class__1578            generation_chunk = ChatGenerationChunk(1579                message=message_chunk, generation_info=generation_info or None1580            )1581            if run_manager:1582                await run_manager.on_llm_new_token(1583                    token=generation_chunk.text,1584                    chunk=generation_chunk,1585                    logprobs=logprobs,1586                )1587            yield generation_chunk1588        if last_usage:1589            usage_chunk = self._usage_generation_chunk(last_usage)1590            if run_manager:1591                await run_manager.on_llm_new_token("", chunk=usage_chunk)1592            yield usage_chunk15931594    async def _agenerate(1595        self,1596        messages: list[BaseMessage],1597        stop: list[str] | None = None,1598        run_manager: AsyncCallbackManagerForLLMRun | None = None,1599        stream: bool | None = None,  # noqa: FBT0011600        **kwargs: Any,1601    ) -> ChatResult:1602        should_stream = stream if stream is not None else self.streaming1603        if should_stream:1604            stream_iter = self._astream(1605                messages, stop=stop, run_manager=run_manager, **kwargs1606            )1607            if self.output_version == "v1":1608                stream_iter = _afinalize_stream(stream_iter)1609            return await agenerate_from_stream(stream_iter)16101611        message_dicts, params = self._create_message_dicts(messages, stop)1612        params = {1613            **params,1614            **({"stream": stream} if stream is not None else {}),1615            **kwargs,1616        }1617        try:1618            response = await _acompletion_with_retry(1619                self, run_manager=run_manager, messages=message_dicts, **params1620            )1621        except BadRequestError as e:1622            _handle_fireworks_invalid_request(e)1623        except APIError as e:1624            _handle_fireworks_api_error(e)1625        return self._create_chat_result(response)16261627    @property1628    def _identifying_params(self) -> dict[str, Any]:1629        """Get the identifying parameters."""1630        return {"model_name": self.model_name, **self._default_params}16311632    def _get_invocation_params(1633        self, stop: list[str] | None = None, **kwargs: Any1634    ) -> dict[str, Any]:1635        """Get the parameters used to invoke the model."""1636        return {1637            "model": self.model_name,1638            **super()._get_invocation_params(stop=stop),1639            **self._default_params,1640            **kwargs,1641        }16421643    @property1644    def _llm_type(self) -> str:1645        """Return type of chat model."""1646        return "fireworks-chat"16471648    def bind_tools(1649        self,1650        tools: Sequence[dict[str, Any] | type[BaseModel] | Callable | BaseTool],1651        *,1652        tool_choice: dict | str | bool | None = None,1653        **kwargs: Any,1654    ) -> Runnable[LanguageModelInput, AIMessage]:1655        """Bind tool-like objects to this chat model.16561657        Assumes model is compatible with Fireworks tool-calling API.16581659        Args:1660            tools: A list of tool definitions to bind to this chat model.16611662                Supports any tool definition handled by [`convert_to_openai_tool`][langchain_core.utils.function_calling.convert_to_openai_tool].1663            tool_choice: Which tool to require the model to call.1664                Must be the name of the single provided function,1665                `'auto'` to automatically determine which function to call1666                with the option to not call any function, `'any'` to enforce that some1667                function is called, or a dict of the form:1668                `{"type": "function", "function": {"name": <<tool_name>>}}`.1669            **kwargs: Any additional parameters to pass to1670                `langchain_fireworks.chat_models.ChatFireworks.bind`1671        """  # noqa: E5011672        strict = kwargs.pop("strict", None)1673        formatted_tools = [1674            convert_to_openai_tool(tool, strict=strict) for tool in tools1675        ]1676        if tool_choice is not None and tool_choice:1677            if isinstance(tool_choice, str) and (1678                tool_choice not in ("auto", "any", "none")1679            ):1680                tool_choice = {"type": "function", "function": {"name": tool_choice}}1681            if isinstance(tool_choice, bool):1682                if len(tools) > 1:1683                    msg = (1684                        "tool_choice can only be True when there is one tool. Received "1685                        f"{len(tools)} tools."1686                    )1687                    raise ValueError(msg)1688                tool_name = formatted_tools[0]["function"]["name"]1689                tool_choice = {1690                    "type": "function",1691                    "function": {"name": tool_name},1692                }16931694            kwargs["tool_choice"] = tool_choice1695        return super().bind(tools=formatted_tools, **kwargs)16961697    def with_structured_output(1698        self,1699        schema: dict | type[BaseModel] | None = None,1700        *,1701        method: Literal[1702            "function_calling", "json_mode", "json_schema"1703        ] = "function_calling",1704        include_raw: bool = False,1705        **kwargs: Any,1706    ) -> Runnable[LanguageModelInput, dict | BaseModel]:1707        """Model wrapper that returns outputs formatted to match the given schema.17081709        Args:1710            schema: The output schema. Can be passed in as:17111712                - An OpenAI function/tool schema,1713                - A JSON Schema,1714                - A `TypedDict` class,1715                - Or a Pydantic class.17161717                If `schema` is a Pydantic class then the model output will be a1718                Pydantic instance of that class, and the model-generated fields will be1719                validated by the Pydantic class. Otherwise the model output will be a1720                dict and will not be validated.17211722                See `langchain_core.utils.function_calling.convert_to_openai_tool` for1723                more on how to properly specify types and descriptions of schema fields1724                when specifying a Pydantic or `TypedDict` class.17251726            method: The method for steering model generation, one of:17271728                - `'function_calling'`:1729                    Uses Fireworks's [tool-calling features](https://docs.fireworks.ai/guides/function-calling).1730                - `'json_schema'`:1731                    Uses Fireworks's [structured output feature](https://docs.fireworks.ai/structured-responses/structured-response-formatting).1732                - `'json_mode'`:1733                    Uses Fireworks's [JSON mode feature](https://docs.fireworks.ai/structured-responses/structured-response-formatting).17341735                !!! warning "Behavior changed in `langchain-fireworks` 0.2.8"17361737                    Added support for `'json_schema'`.17381739            include_raw:1740                If `False` then only the parsed structured output is returned.17411742                If an error occurs during model output parsing it will be raised.17431744                If `True` then both the raw model response (a `BaseMessage`) and the1745                parsed model response will be returned.17461747                If an error occurs during output parsing it will be caught and returned1748                as well.17491750                The final output is always a `dict` with keys `'raw'`, `'parsed'`, and1751                `'parsing_error'`.17521753            kwargs:1754                Any additional parameters to pass to the `langchain.runnable.Runnable`1755                constructor.17561757        Returns:1758            A `Runnable` that takes same inputs as a1759                `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is1760                `False` and `schema` is a Pydantic class, `Runnable` outputs an instance1761                of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is1762                `False` then `Runnable` outputs a `dict`.17631764                If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:17651766                - `'raw'`: `BaseMessage`1767                - `'parsed'`: `None` if there was a parsing error, otherwise the type1768                    depends on the `schema` as described above.1769                - `'parsing_error'`: `BaseException | None`17701771        Example: schema=Pydantic class, method="function_calling", include_raw=False:17721773        ```python1774        from typing import Optional17751776        from langchain_fireworks import ChatFireworks1777        from pydantic import BaseModel, Field177817791780        class AnswerWithJustification(BaseModel):1781            '''An answer to the user question along with justification for the answer.'''17821783            answer: str1784            # If we provide default values and/or descriptions for fields, these will be passed1785            # to the model. This is an important part of improving a model's ability to1786            # correctly return structured outputs.1787            justification: str | None = Field(1788                default=None, description="A justification for the answer."1789            )179017911792        model = ChatFireworks(1793            model="accounts/fireworks/models/gpt-oss-120b",1794            temperature=0,1795        )1796        structured_model = model.with_structured_output(AnswerWithJustification)17971798        structured_model.invoke(1799            "What weighs more a pound of bricks or a pound of feathers"1800        )18011802        # -> AnswerWithJustification(1803        #     answer='They weigh the same',1804        #     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'1805        # )1806        ```18071808        Example: schema=Pydantic class, method="function_calling", include_raw=True:18091810        ```python1811        from langchain_fireworks import ChatFireworks1812        from pydantic import BaseModel181318141815        class AnswerWithJustification(BaseModel):1816            '''An answer to the user question along with justification for the answer.'''18171818            answer: str1819            justification: str182018211822        model = ChatFireworks(1823            model="accounts/fireworks/models/gpt-oss-120b",1824            temperature=0,1825        )1826        structured_model = model.with_structured_output(1827            AnswerWithJustification, include_raw=True1828        )18291830        structured_model.invoke(1831            "What weighs more a pound of bricks or a pound of feathers"1832        )1833        # -> {1834        #     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),1835        #     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),1836        #     'parsing_error': None1837        # }1838        ```18391840        Example: schema=TypedDict class, method="function_calling", include_raw=False:18411842        ```python1843        from typing_extensions import Annotated, TypedDict18441845        from langchain_fireworks import ChatFireworks184618471848        class AnswerWithJustification(TypedDict):1849            '''An answer to the user question along with justification for the answer.'''18501851            answer: str1852            justification: Annotated[1853                str | None, None, "A justification for the answer."1854            ]185518561857        model = ChatFireworks(1858            model="accounts/fireworks/models/gpt-oss-120b",1859            temperature=0,1860        )1861        structured_model = model.with_structured_output(AnswerWithJustification)18621863        structured_model.invoke(1864            "What weighs more a pound of bricks or a pound of feathers"1865        )1866        # -> {1867        #     'answer': 'They weigh the same',1868        #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'1869        # }1870        ```18711872        Example: schema=OpenAI function schema, method="function_calling", include_raw=False:18731874        ```python1875        from langchain_fireworks import ChatFireworks18761877        oai_schema = {1878            "name": "AnswerWithJustification",1879            "description": "An answer to the user question along with justification for the answer.",1880            "parameters": {1881                "type": "object",1882                "properties": {1883                    "answer": {"type": "string"},1884                    "justification": {1885                        "description": "A justification for the answer.",1886                        "type": "string",1887                    },1888                },1889                "required": ["answer"],1890            },1891        }18921893        model = ChatFireworks(1894            model="accounts/fireworks/models/gpt-oss-120b",1895            temperature=0,1896        )1897        structured_model = model.with_structured_output(oai_schema)18981899        structured_model.invoke(1900            "What weighs more a pound of bricks or a pound of feathers"1901        )1902        # -> {1903        #     'answer': 'They weigh the same',1904        #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'1905        # }1906        ```19071908        Example: schema=Pydantic class, method="json_mode", include_raw=True:19091910        ```python1911        from langchain_fireworks import ChatFireworks1912        from pydantic import BaseModel191319141915        class AnswerWithJustification(BaseModel):1916            answer: str1917            justification: str191819191920        model = ChatFireworks(1921            model="accounts/fireworks/models/gpt-oss-120b", temperature=01922        )1923        structured_model = model.with_structured_output(1924            AnswerWithJustification, method="json_mode", include_raw=True1925        )19261927        structured_model.invoke(1928            "Answer the following question. "1929            "Make sure to return a JSON blob with keys 'answer' and 'justification'. "1930            "What's heavier a pound of bricks or a pound of feathers?"1931        )1932        # -> {1933        #     'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'),1934        #     'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'),1935        #     'parsing_error': None1936        # }1937        ```19381939        Example: schema=None, method="json_mode", include_raw=True:19401941        ```python1942        structured_model = model.with_structured_output(1943            method="json_mode", include_raw=True1944        )19451946        structured_model.invoke(1947            "Answer the following question. "1948            "Make sure to return a JSON blob with keys 'answer' and 'justification'. "1949            "What's heavier a pound of bricks or a pound of feathers?"1950        )1951        # -> {1952        #     'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'),1953        #     'parsed': {1954        #         'answer': 'They are both the same weight.',1955        #         'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'1956        #     },1957        #     'parsing_error': None1958        # }1959        ```19601961        """  # noqa: E5011962        _ = kwargs.pop("strict", None)1963        if kwargs:1964            msg = f"Received unsupported arguments {kwargs}"1965            raise ValueError(msg)1966        is_pydantic_schema = _is_pydantic_class(schema)1967        if method == "function_calling":1968            if schema is None:1969                msg = (1970                    "schema must be specified when method is 'function_calling'. "1971                    "Received None."1972                )1973                raise ValueError(msg)1974            formatted_tool = convert_to_openai_tool(schema)1975            tool_name = formatted_tool["function"]["name"]1976            llm = self.bind_tools(1977                [schema],1978                tool_choice=tool_name,1979                ls_structured_output_format={1980                    "kwargs": {"method": "function_calling"},1981                    "schema": formatted_tool,1982                },1983            )1984            if is_pydantic_schema:1985                output_parser: OutputParserLike = PydanticToolsParser(1986                    tools=[schema],  # type: ignore[list-item]1987                    first_tool_only=True,  # type: ignore[list-item]1988                )1989            else:1990                output_parser = JsonOutputKeyToolsParser(1991                    key_name=tool_name, first_tool_only=True1992                )1993        elif method == "json_schema":1994            if schema is None:1995                msg = (1996                    "schema must be specified when method is 'json_schema'. "1997                    "Received None."1998                )1999                raise ValueError(msg)2000            formatted_schema = convert_to_json_schema(schema)

Code quality findings 2

Ensure functions have docstrings for documentation
info maintainability missing-docstring
def lc_secrets(self) -> dict[str, str]:
Ensure functions have docstrings for documentation
info maintainability missing-docstring
def lc_attributes(self) -> dict[str, Any]:

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings