@@ -404,60 +404,73 @@ def ask_question(self, question, conversation_id: Optional[str] = None):
404404 Returns:
405405 GenieResponse with result, query, description, and conversation_id
406406 """
407- args = {"query" : question }
408- if conversation_id :
409- args ["conversation_id" ] = conversation_id
407+ try :
408+ # Try using the new MCP approach
409+ args = {"query" : question }
410+ if conversation_id :
411+ args ["conversation_id" ] = conversation_id
410412
411- mcp_result = self ._mcp_client .call_tool (self ._tool_name , args )
413+ mcp_result = self ._mcp_client .call_tool (self ._tool_name , args )
412414
413- if not mcp_result .content or len (mcp_result .content ) == 0 :
414- return GenieResponse (
415- result = "No content returned from Genie" ,
416- conversation_id = conversation_id ,
417- )
415+ if not mcp_result .content or len (mcp_result .content ) == 0 :
416+ return GenieResponse (
417+ result = "No content returned from Genie" ,
418+ conversation_id = conversation_id ,
419+ )
418420
419- # Genie backend always returns exactly 1 content block with JSON
420- content_block = mcp_result .content [0 ]
421- content_text = content_block .text if hasattr (content_block , "text" ) else "{}"
421+ # Genie backend always returns exactly 1 content block with JSON
422+ content_block = mcp_result .content [0 ]
423+ content_text = content_block .text if hasattr (content_block , "text" ) else "{}"
422424
423- try :
424- genie_response = json .loads (content_text )
425- except json .JSONDecodeError :
426- return GenieResponse (
427- result = f"Failed to parse response: { content_text } " ,
428- conversation_id = conversation_id ,
429- )
425+ try :
426+ genie_response = json .loads (content_text )
427+ except json .JSONDecodeError :
428+ return GenieResponse (
429+ result = f"Failed to parse response: { content_text } " ,
430+ conversation_id = conversation_id ,
431+ )
430432
431- content = genie_response .get ("content" , "" )
432- conv_id = genie_response .get ("conversationId" , conversation_id )
433- status = genie_response .get ("status" , "" )
433+ content = genie_response .get ("content" , "" )
434+ conv_id = genie_response .get ("conversationId" , conversation_id )
435+ status = genie_response .get ("status" , "" )
436+
437+ try :
438+ content_data = json .loads (content )
439+ query_str = content_data .get ("query" , "" )
440+ description = content_data .get ("description" , "" )
441+ statement_response = content_data .get ("statement_response" )
442+
443+ if (
444+ statement_response
445+ and statement_response .get ("status" , {}).get ("state" ) == "SUCCEEDED"
446+ ):
447+ result = _parse_query_result (
448+ statement_response , self .truncate_results , self .return_pandas
449+ )
450+ else :
451+ result = content
452+ query_str = ""
453+ description = ""
434454
435- try :
436- content_data = json .loads (content )
437- query_str = content_data .get ("query" , "" )
438- description = content_data .get ("description" , "" )
439- statement_response = content_data .get ("statement_response" )
440-
441- if (
442- statement_response
443- and statement_response .get ("status" , {}).get ("state" ) == "SUCCEEDED"
444- ):
445- result = _parse_query_result (
446- statement_response , self .truncate_results , self .return_pandas
447- )
448- else :
455+ except (json .JSONDecodeError , KeyError , TypeError , AttributeError ):
449456 result = content
450457 query_str = ""
451458 description = ""
452459
453- except (json .JSONDecodeError , KeyError , TypeError , AttributeError ):
454- result = content
455- query_str = ""
456- description = ""
460+ return GenieResponse (
461+ result = result ,
462+ query = query_str ,
463+ description = description ,
464+ conversation_id = conv_id ,
465+ )
457466
458- return GenieResponse (
459- result = result ,
460- query = query_str ,
461- description = description ,
462- conversation_id = conv_id ,
463- )
467+ except Exception as e :
468+ # Fall back to deprecated REST methods
469+ logging .warning (f"MCP call failed, falling back to REST API: { e } " )
470+
471+ if conversation_id :
472+ resp = self .create_message (conversation_id , question )
473+ return self .poll_for_result (conversation_id , resp ["id" ])
474+ else :
475+ resp = self .start_conversation (question )
476+ return self .poll_for_result (resp ["conversation_id" ], resp ["id" ])
0 commit comments