Skip to content

Conversation

@KUNDAN1334
Copy link

solved #7

@bnarasimha21 bnarasimha21 requested a review from Copilot October 28, 2025 12:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds timeout and retry handling capabilities to the langchain-gradient library. The changes implement configurable timeout and retry behavior for API requests, along with proper error handling and documentation.

  • Adds custom exception classes (APITimeoutError, APIRetryError) for better error handling
  • Implements a RetryConfig class with exponential backoff support
  • Updates ChatGradient to support timeout and retry configuration through the Gradient SDK
  • Adds comprehensive test coverage for timeout and retry scenarios

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/unit_tests/test_timeout_retry.py New test file covering timeout configuration, retry logic, and error handling scenarios
langchain_gradient/retry_config.py New module implementing retry configuration with exponential backoff
langchain_gradient/exceptions.py New module defining custom exception classes for timeout and retry errors
langchain_gradient/chat_models.py Updated to support timeout/retry configuration and enhanced error handling with gradient SDK exceptions
docs/timeout_retry-examples.md New documentation file with usage examples for timeout and retry configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +53 to +56
raise APIRetryError(
f"Failed after {retry_config.max_retries + 1} attempts",
attempts=retry_config.max_retries + 1
) from last_exception
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The APIRetryError exception is used but not imported. This will cause a NameError at runtime when the max retries are exceeded.

Copilot uses AI. Check for mistakes.
max_retries: int = 2
"""Max number of retries."""
timeout: Optional[float] = Field(default=60.0)
"""Timeout for requests in seconds. Defaults to 60.0 seconds."""
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring indicates timeout defaults to 60.0 seconds, but the class-level docstring at line 73 states it 'Defaults to 60.0 seconds (Gradient SDK default)'. The actual Gradient SDK default should be verified, as the code now explicitly sets 60.0 instead of allowing the SDK to use its own default. Consider using None if you want to rely on the SDK's default, or clarify that this is now a library-specific default.

Suggested change
"""Timeout for requests in seconds. Defaults to 60.0 seconds."""
"""Timeout for requests in seconds. Defaults to 60.0 seconds (library default)."""

Copilot uses AI. Check for mistakes.
from typing import Any, Dict, Iterator, List, Optional, Union

from gradient import Gradient
import gradient
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gradient module is imported twice: once at line 6 as from gradient import Gradient and again at line 7 as import gradient. The second import is only used for exception handling. Consider organizing imports more clearly or adding a comment explaining why both import styles are needed.

Copilot uses AI. Check for mistakes.
Comment on lines +5 to +44
from langchain_gradient import ChatGradient
from langchain_gradient.exceptions import APITimeoutError, APIRetryError

Initialize with timeout and retry configuration
llm = ChatGradient(
model="llama3.3-70b-instruct",
timeout=30.0,
max_retries=3
)

try:
response = llm.invoke("Hello world!")
print(response)
except APITimeoutError as e:
print(f"Request timed out: {e}")
except APIRetryError as e:
print(f"Max retries exceeded: {e}")


## Advanced Configuration

from langchain_gradient import ChatGradient
from langchain_gradient.retry_config import RetryConfig

Custom retry configuration with exponential backoff
custom_retry = RetryConfig(
max_retries=5,
initial_delay=1.0,
max_delay=60.0,
exponential_base=2.0,
jitter=True
)

llm = ChatGradient(
model="llama3.3-70b-instruct",
timeout=60.0,
retry_config=custom_retry
)


Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing markdown code fence markers around the Python code. The code examples should be wrapped in triple backticks with the python language identifier for proper rendering.

Suggested change
from langchain_gradient import ChatGradient
from langchain_gradient.exceptions import APITimeoutError, APIRetryError
Initialize with timeout and retry configuration
llm = ChatGradient(
model="llama3.3-70b-instruct",
timeout=30.0,
max_retries=3
)
try:
response = llm.invoke("Hello world!")
print(response)
except APITimeoutError as e:
print(f"Request timed out: {e}")
except APIRetryError as e:
print(f"Max retries exceeded: {e}")
## Advanced Configuration
from langchain_gradient import ChatGradient
from langchain_gradient.retry_config import RetryConfig
Custom retry configuration with exponential backoff
custom_retry = RetryConfig(
max_retries=5,
initial_delay=1.0,
max_delay=60.0,
exponential_base=2.0,
jitter=True
)
llm = ChatGradient(
model="llama3.3-70b-instruct",
timeout=60.0,
retry_config=custom_retry
)
```python
from langchain_gradient import ChatGradient
from langchain_gradient.exceptions import APITimeoutError, APIRetryError
# Initialize with timeout and retry configuration
llm = ChatGradient(
model="llama3.3-70b-instruct",
timeout=30.0,
max_retries=3
)
try:
response = llm.invoke("Hello world!")
print(response)
except APITimeoutError as e:
print(f"Request timed out: {e}")
except APIRetryError as e:
print(f"Max retries exceeded: {e}")

Advanced Configuration

from langchain_gradient import ChatGradient
from langchain_gradient.retry_config import RetryConfig

# Custom retry configuration with exponential backoff
custom_retry = RetryConfig(
    max_retries=5,
    initial_delay=1.0,
    max_delay=60.0,
    exponential_base=2.0,
    jitter=True
)

llm = ChatGradient(
    model="llama3.3-70b-instruct",
    timeout=60.0,
    retry_config=custom_retry
)

Copilot uses AI. Check for mistakes.
attempts=retry_config.max_retries + 1
) from last_exception

return None
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return statement is unreachable because the for loop will either return successfully or raise an exception. Remove this line to improve code clarity.

Suggested change
return None

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant